Reputation: 1594
I'm using BindingListView
recommended here Generic IBindingListView Implementations and I wanted to apply a composite predicate as a BidingListView
filter:
var lst = new List<DemoClass>
{
new DemoClass { Prop1 = "a", Prop2 = "b", Prop3 = "c" },
new DemoClass { Prop1 = "a", Prop2 = "e", Prop3 = "f" },
new DemoClass { Prop1 = "b", Prop2 = "h", Prop3 = "i" },
new DemoClass { Prop1 = "b", Prop2 = "k", Prop3 = "l" }
};
dataGridView1.DataSource = new BindingListView<DemoClass>(lst);
var view = (BindingListView<DemoClass>)dataGridView1.DataSource;
view.ApplyFilter(dc =>
dc.Prop1 == "a" &&
dc.Prop2 == "b" &&
dc.Prop3 == "f");
But unlike in the above example the composite predicate should be built from the list of predicates, e.g., as the following:
var predicates = new List<Predicate<DemoClass>>()
{
dc => dc.Prop1 == "a",
dc => dc.Prop2 == "e",
dc => dc.Prop3 == "f"
};
How can I do that:
view.ApplyFilter( ? predicates ? );
Upvotes: 0
Views: 26
Reputation: 1594
I have made it solved like that:
public static class MyExtension
{
public static Predicate<T> And<T>(this Predicate<T> left, Predicate<T> right)
{
return a => left(a) && right(a);
}
}
...
and then:
Predicate<DemoClass> compositePredicate = x => true;
compositePredicate = predicates.Aggregate(compositePredicate, (acc, x) => acc.And(x));
view.ApplyFilter(compositePredicate);
Upvotes: 0