My linq query to summarize something like -
string CustomerID;// can be "ALL" or any value
var itemlist = ( from itmhstry in context.ItemHistories
join itm in context.Items on itmhstry.ItemID equals itm.ItemID into itm_join
where itmhstry.CustomerID == CustomerID
.......................)
and query goes on to select values needed
here how to select all values (like select * >> without filter) when CustomerID
value is ALL/NULL.? How to frame the where clause for this purpose?
I can rewrite same query with if else to have two different queries to handle this issue but is there any simpler way to do?
Try this:
If
CustomerID
is empty or null or "ALL", then either the first or the second predicate in thewhere
clause evaluate totrue
and no filtering is applied. IfCustomerID
is not empty AND not null AND not "ALL", then you end up with the initial query.