Search code examples
c#asp.netlinqentity-framework-6linq-to-entities

How to get a filtered List?


This is the scenario. I have the next class:

class A
{ 
    string  attribute1; 
    string  attribute2;
    List<B> attribute3;
}

class B 
{
    string attribute1;
}

And my program runs a:

list<Class A>    myList

I want to get, filtering using linq, a specific list Class A

So, as far as I reach, I am getting a list or a list through:

myList.SelectMany(o => o.attribute3.Where(p => p.attribute1 == "test")).ToList()

myList.SelectMany(o => o.attribute3.Select(p => p.attribute1 == "test")).ToList()

Any clue? Thanks mates.


Solution

  • Just run:

    myList.Where(o => o.attribute3.Any(p => p.attribute1 == "test")).ToList()