How do i construct a LINQ WHERE
clause that contains OR
?
i have a list of objects, and i want to return those that match a search criteria.
The contained objects have many properties, and as long as any match the criteria, i want to return it:
IEnumerable<Item> list;
String keyword;
...
var results = list.Where(
(item => item.Name.Contains(keyword, StringComparison.CurrentCultureIgnoreCase))
||
(item => item.Description.Contains(keyword, StringComparison.CurrentCultureIgnoreCase))
||
(item => item.Description.Contains(keyword, StringComparison.CurrentCultureIgnoreCase))
||
(item => item.ItemType.Contains(keyword, StringComparison.CurrentCultureIgnoreCase))
||
(item => item.ItemID.ToString().StartsWith(keyword, StringComparison.CurrentCultureIgnoreCase))
||
(items => items.Value.ToString().StartsWith(keyword, StringComparison.CurrentCultureIgnoreCase))
);
But that fails to compile:
Operator '||' cannot be applied to operands of type 'lambda expression' and 'lambda expression'
How do i construct a LINQ WHERE
clause that contains OR
?
See also
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…