skip to Main Content

I’m facing a issue when trying to search my page when trying to filter from 5 parameters.

If the parameters is null then I would want to ignore that search and only return results if there is a value in the parameters.

At the moment i used separate parameters but i need something that caters for all 5 and if it has 2 out of 4 values then it should return based on the filter.:

if (name != null)
{
    report = report.Where(asd => asd.name == name).OrderBy(x => x.Id).ToList();
}
else if (Id != null)
{
    report = report.Where(x => x.Id == Id).OrderBy(x => x.Id).ToList();
}

So this works with only 1 in mind, how would i join all 4 together so it works together.

This is my full code:

[HttpGet]
public ActionResult Get(string Id = null, string name = null, string status = null, string startDate = null, string endDate = null)
{
    List<ReportModel> report = new List<ReportModel>();
    report = (from data in _context.ReportData
                //where data.Id== Id
                //&& data.name== name
                //&& data.Status == status
                //&& data.Date >= Convert.ToDateTime(startDate) && data.Date < Convert.ToDateTime(endDate)
                select new ReportModel
                {
                    Id = data.Id,
                    Name = data.Plant,
                    Status = data.Status,
                    Date = data.Date
                }).ToList();

    if (name != null)
    {
        report = report.Where(asd => asd.name == name).OrderBy(x => x.Id).ToList();
    }
    else if (Id != null)
    {
        report = report.Where(x => x.Id == Id).OrderBy(x => x.Id).ToList();
    }
    else if (status != null)
    {
        report = report.Where(x => x.Status == status).OrderBy(x => x.Id).ToList();
    }

    return Ok(report);
}

Kindly help.

2

Answers


  1. If you care about performance, do not call ToList() early, because it will load whole table into the memory. IQueryable can be combined to produce desired result.

    [HttpGet]
    public ActionResult Get(string Id = null, string name = null, string status = null, string startDate = null, string endDate = null)
    {
        var query = _context.ReportData.AsQueryable();
    
        if (name != null)
        {
            query = query.Where(asd => asd.name == name);
        }
    
        if (Id != null)
        {
            query = query.Where(x => x.Id == Id);
        }
    
        if (status != null)
        {
            query = query.Where(x => x.Status == status);
        }
    
        query = query.OrderBy(x => x.Id);
    
        var report = query
            .Select(data => new ReportModel
            {
                Id = data.Id,
                Name = data.Plant,
                Status = data.Status,
                Date = data.Date
            })
            .ToList();
    
        return Ok(report);
    }
    
    Login or Signup to reply.
  2. Intead of using if condition, you can add directly the condition in the Where clause

    if (name != null)
    {
        query = query.Where(asd => asd.name == name);
    }
    // replace by
    query = query.Where(asd => asd.name == name || name == null);
    

    when name will be null, the Where condition will always true.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search