I have a List<B>
and class B
has a public property A
.
How can I filter the list of B
using a Predicate<A>
?
If your list is named myList and your predicate myPredicate, then you want probably something like this:
var result = myList.Where(b => myPredicate(b.A))
or you can use a lambda expression, e.g.:
var result = myList.Where(b => b.A < 10)
or something like this, where the predicate would be b.A < 10
.
Maybe you should give us a more complete code sample.
myList.FindAll(b=>yourPredicate(b.A))
thanks
Mar 12, 2012 at 12:30
If you are using Linq, you can do this:
// Define the predicate inline:
var results = list.Where(b => b.A.Name == "Fred");
// or if you have the predicate defined separately:
var results = list.Where(b => predicate(b.A));
If you are not using Linq, you can use the FindAll method on the System.Collections.Generic.List class:
var results = list.FindAll(predicate);