KeldJ
KeldJ

Reputation: 141

Filter childcollection by List

I have an simpel model

public class A 
{
    public int ID {get;set;}
    public string Title {get;set;}
    public List<B> Bzzzz {get;set;}
}

 public class B 
{
    public int ID {get;set;}
    public string Title {get;set;}
}

In linq i would like to filter a List by a List like. Select all from List where single Bzzzz in List se code.

List<A> aList = ....
List<B> bList = ....

aList.Where(x => x.Bzzzz.Any(y => bList.Contains(y)));

The returned collection is always empty, whats wrong

Upvotes: 1

Views: 51

Answers (1)

Jawad
Jawad

Reputation: 11364

You are trying to do is compare the two list objects. You are better off comparing the values of the IDs than the objects themselves.. which wont be true because they are two different references.

aList.Where(x => x.Bzzzz.Any(y => bList.Any(z => z.ID == y.ID)));

Upvotes: 1

Related Questions