asp.net mvc - Advanced Search in LINQ Query -
i want make advanced search (filtering) using different criteria. example, if first name available query should return available matches. if first name , last name both available should match first name or last name , similar way many criteria (gender, profession, education etc.)
here controller method in taking ajax data parameters.
[httppost] public actionresult search(string cno, string fname, string lname, string male, string female, string stateid, string cityid, string professionid, string educationid) { if (request.isajaxrequest()) { var db = new clubdatacontext(); /*----------advanced search query-------------*/ return partialview("serachresult"); } else return redirecttoaction("index", "home"); }
it may possible parameters have null value. please suggest linq query suitable in situation.
you have option use c# null-coalescing operator
users.where(x => x.firstname == (fname??x.firstname) || x.lastname == (lname?? x.lastname)).tolist();
further
users.where(x => x.firstname.contains(fname??x.firstname) || x.lastname.contains(lname?? x.lastname)).tolist();
equivalent sql :
select * user u u.firstname = isnull(@fname,u.firstname) or u.lastname = isnull(@lname,u.lastname)
Comments
Post a Comment