I need to write a LINQ Query against a returned list that takes in multiple optional parameters. There will be the following variables:
plan, Id, FirstName, LastName, DateFrom, DateTo, MemDateOfBirth
I want to return a filtered list back via LINQ using these parameters, but all of them are optional. At least one will be supplied when the user hits the search button, but that will be up to the user to decide what they want to search by. If they supply more than 1, I need to filter by all methods they supply...
So for instance, if they supply a first name and to and from dates, I want to return a filtered list of all instances for a person by that first name between the to and from dates, etc...
What is the easiest way to accomplish this with LINQ? These variables are optional parameters, so any or all of them could be supplied. I know I could return the main list, then filter through it multiple times to get the results, but I was wondering if there was a quicker, easier way to do it via LINQ...
thanks in advance for your help!
i found this to be the simplest way to solve this type of problem
var q = from mt in myTable
where (mt.FIrstname == FirstNameparam || FirstNameparam == null)
&& (mt.lastname == lastnameParam || lastnameParam == null)
&& (mt.DateField == DateParam || DateParam == null)
select new
{
mt.FIrstname,
mt.lastname,
mt.DateField
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With