Tim Almond
Tim Almond

Reputation: 12726

Lost with LINQ and Expressions

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

Answers (3)

Sebastien Robert
Sebastien Robert

Reputation: 331

I'm not sure I understand your question, but you can use predicate builder. Predicate Builder example here

Upvotes: 1

jball
jball

Reputation: 25024

var query = Somthing().Where(x => x.IsSomethingYouAlwaysFilterBy);
if(FilterValueIsSet(filterValue))
{
    query = query.Where(x => x.Property == filterValue)
}

Upvotes: 1

Jackson Pope
Jackson Pope

Reputation: 14660

Use where to find the items of interest, e.g.:

collection.Where(i => PassesFilter(i)).Select(i => i.InterestingValue);

Upvotes: 4

Related Questions