I have a List of custom objects:
List<SomeObject> someobjects = getAllSomeObjects();
List<SomeObject> someobjectsfiltered = new List<SomeObject>();
class SomeObject
{
List <AnotherList>
}
class AnotherList
{
public string Name{get;set;}
public Categories category {get;set;}
}
So I'm trying to get All AnotherList items of a specific type using Lambda
someobjectsfiltered = someobjects.SelectMany(s => s.AnotherList.FindAll(a => a.category == SomeCategory));
But I get the
Can not implicitly convert type IEnumerable to Generic.List
error
Any idea how to solve this?
Many thanks.
You need to throw a ToList()
on the end or change the type of the result to IEnumerable<SomeObject>
because, as the error says, you can't assign an IEnumerable<T>
to a variable of type List<T>
.
someobjectsfiltered = someobjects.SelectMany(s => s.AnotherList.FindAll(a => a.category == SomeCategory))
.ToList();
Edit based on comments
If what you want is the SomeObjects
that have a list containing an item that matches the category you can do that using.
someobjectsfiltered = someobjects.Where( s => s.AnotherList.Any( a => a.category == SomeCategory ))
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With