Reputation: 12726
I'm trying to write a LINQ query on some objects where I need to only do a select if a filter value is set.
Is there a way to "change" the query dynamically to only do a select if this is set.
Upvotes: 1
Views: 139
Reputation: 331
I'm not sure I understand your question, but you can use predicate builder. Predicate Builder example here
Upvotes: 1
Reputation: 25024
var query = Somthing().Where(x => x.IsSomethingYouAlwaysFilterBy);
if(FilterValueIsSet(filterValue))
{
query = query.Where(x => x.Property == filterValue)
}
Upvotes: 1
Reputation: 14660
Use where
to find the items of interest, e.g.:
collection.Where(i => PassesFilter(i)).Select(i => i.InterestingValue);
Upvotes: 4