Home > database >  is there a simpler way to write my code instead of manually writing each if else when I add a parame
is there a simpler way to write my code instead of manually writing each if else when I add a parame

Time:08-29

I'm trying to do a search function but I wanna add all possibilities that user can do in the search but I'm still a beginner so I want to know if there is a simpler way to do it instead of writing else if on each parameter.

  public JsonResult getAll( string devname, string status1 , string pnumber)
        {
            List<SavePhone> reportList = new List<SavePhone>();
            List<SavePhone> p = new List<SavePhone>();
            p = GetAllReports();
            
            if (devname=="nulls" && status1 == "nulls" && pnumber=="nulls" )
            {
                return Json(p, JsonRequestBehavior.AllowGet);
            }
            else if(devname != "nulls" && status1 != "nulls" && pnumber != "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.DeviceName == devname && x.PNB == pnumber && x.Status==status1).ToList();
                }
            } else if(devname != "nulls" && status1 != "nulls" && pnumber == "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.DeviceName == devname && x.Status == status1).ToList();
                }

            }
            else if (devname != "nulls" && status1 == "nulls" && pnumber != "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.DeviceName == devname && x.PNB == pnumber).ToList();
                }
            }
            else if (devname == "nulls" && status1 != "nulls" && pnumber != "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.PNB == pnumber && x.Status == status1).ToList();
                }
            }
            else if(devname != "nulls" && status1 == "nulls" && pnumber == "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.DeviceName == devname).ToList();
                }

            }
            else if (devname == "nulls" && status1 != "nulls" && pnumber == "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.Status == status1).ToList();
                }

            }
            else if (devname == "nulls" && status1 == "nulls" && pnumber != "nulls")
            {
                for (int i = 0; i < p.Count; i  )
                {
                    reportList = p.Where(x => x.PNB == pnumber).ToList();
                }

            }
         
            var JsonResult = Json(reportList, JsonRequestBehavior.AllowGet);
            return JsonResult;
        } 

is there a way that the code automatically does it for me without having to add each one manually because if I have like 10 parameters it would take forever.

CodePudding user response:

You can build your LINQ query by chaining a .Where for every parameter.

IEnumerable<SavePhone> query = p;

if (devname != 'nulls') 
{
  query = query.Where(x => x.DeviceName == devname);
}

if (status1 != 'nulls')
{
  query = query.Where(x => x.Status1 == status1);
}

//etc.

var reportList = query.ToList();

I'm not sure why you have that for loop and I my opinion it's not needed.

  • Related