0

I have 2 classes

public class ClassA
{
    public int Id { get; set; }
    public string Name { get; set; }
}

And

public class ClassB
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ClassA ClassA { get; set; }
}

I am trying to filter a list of ClassB with a List of Class A

void Main()
    {

    var ListA = new List<ClassA>();
        var a1 = new ClassA() {Id=1, Name = "A1"};
    var a2 = new ClassA() {Id=2, Name = "A2"};
    var a3 = new ClassA() {Id=3, Name = "A3"};
    ListA.Add(a1);
    ListA.Add(a2);
    ListA.Add(a3);

    var FilterListA = new List<ClassA>();
    FilterListA.Add(a1);
    FilterListA.Add(a2);


    var ListB = new List<ClassB>();

    var b1 = new ClassB() {Id=1, Name="B1" ,ClassA= a1};
    var b2 = new ClassB() {Id=1, Name="B1", ClassA= a2};
    var b3 = new ClassB() {Id=1, Name="B1", ClassA= a3};
    var b4 = new ClassB() {Id=1, Name="B1", ClassA= a3};

    ListB.Add(b1);
    ListB.Add(b2);
    ListB.Add(b3);
    ListB.Add(b4);

it works if I use

var query = from b in ListB
            join a in FilterListA
            on b.ClassA equals a
            select new { Name = b.Name, ClassAName = a.Name };

Console.WriteLine(query.ToList());

But I would like to do something like this... but i don't know how

Console.WriteLine(ListB.Where(o => o.ClassA IsIncluded In FilterListA));

}

I tried using Contains without success. thanks

Servy
  • 202,030
  • 26
  • 332
  • 449
SerenityNow
  • 1,055
  • 1
  • 15
  • 32
  • 3
    But your `Join` approach is much more efficient than your desired `Where` with contains(or `Any`). http://stackoverflow.com/questions/5551264/why-is-linq-join-so-much-faster-than-linking-with-where – Tim Schmelter Aug 22 '13 at 19:13
  • That was what I was trying to figure out. Join is very fast I must say. I guess I wanted to know different ways of doing the same thing, even if it costs me a couple of reputation points. – SerenityNow Aug 22 '13 at 19:19
  • 20,000,000 iterations of Contains take approx 22 seconds.. using join takes 41 seconds.... and Any takes 43 seconds. So I guess contains is faster... – SerenityNow Aug 22 '13 at 19:28
  • Use meaningful performance tests. The sizes of the collections matter. In my question linked above i have posted some tests. – Tim Schmelter Aug 22 '13 at 19:32
  • good point.... I will keep that in mind – SerenityNow Aug 22 '13 at 19:35

1 Answers1

3

This should works:

ListB.Where(x=>FilterListA.Any(y=>y==x.ClassA));

It gets all elements from ListB which have equivalent element in FilterListA. If object of ClassA included in element of list is listed in FilterListA it's returned.

EDIT: as gunr2171 said you can use Contains

ListB.Where(x=>FilterListA.Contains(x.ClassA));
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • 2
    Alternately you can use `Contains` – gunr2171 Aug 22 '13 at 19:12
  • 1
    Does the phrase "big-O" mean anything to you? – Kennet Belenky Aug 22 '13 at 23:53
  • @KennetBelenky more than you think, but noone said it should be optimized against time complexity. It is valid linq approach that's all. – Kamil Budziewski Aug 23 '13 at 05:51
  • 2
    @wudzik Suppose you're a sixth-grade shop teacher. Do you show the kids how to use a table saw with the blade shroud removed and the pawls disabled? When dealing with beginners, always, always, always, teach them how to do it right. Later on, they can discover on their own how and when to do things wrong. – Kennet Belenky Aug 26 '13 at 18:53