Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering data from parameters c#

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.:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                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.

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading