Search code examples
linqwhere-clauseswitchingclause

Linq Where Clause Change based on Parameters


I have a linq statement that returns a list of records based on where clause
This where clause checks for two parameter values.
Out of which one parameter is optional.
so i need a suggestions if i can switch my where clause based on the optional Parameter
something like this

if(locid==0)
  {
   where (p.CustomerID == custid)
  }
 else{
   where (p.CustomerID == custid) & (p.LocationID == locid )
     }

can any one help me how can i get this work.

thanks


Solution

  • You could try writing it like this:

    where (p.CustomerID == custid) && (locid == 0 || p.LocationID == locid )