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

5
  • 3
    But your Join approach is much more efficient than your desired Where with contains(or Any). stackoverflow.com/questions/5551264/… Commented Aug 22, 2013 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. Commented Aug 22, 2013 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... Commented Aug 22, 2013 at 19:28
  • Use meaningful performance tests. The sizes of the collections matter. In my question linked above i have posted some tests. Commented Aug 22, 2013 at 19:32
  • good point.... I will keep that in mind Commented Aug 22, 2013 at 19:35

1 Answer 1

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)); 
Sign up to request clarification or add additional context in comments.

4 Comments

Alternately you can use Contains
Does the phrase "big-O" mean anything to you?
@KennetBelenky more than you think, but noone said it should be optimized against time complexity. It is valid linq approach that's all.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.