1

I have a list that contains int values, I want to use linq expression to return a result where a property equals the list items values. How can I do that?

list<int> x = ...

var o = anotherList.where(s => s.Id == (the list values));
Vishal Suthar
  • 16,685
  • 2
  • 55
  • 101
Jack Manson
  • 477
  • 1
  • 7
  • 8
  • 5
    What *exactly* do you mean by "where a property equals the list items values". Do you mean where the list *contains* the property value? If so, think about what that might hint at... how can you tell whether a list contains a value? – Jon Skeet Mar 19 '13 at 10:13
  • what should the result be? – default Mar 19 '13 at 10:15

4 Answers4

10
var o = anotherList.Where(s => list.Contains(s.ID));
Grant Thomas
  • 43,307
  • 9
  • 83
  • 125
TalentTuner
  • 17,031
  • 5
  • 37
  • 62
1

I translate "a property equals the list items values" with "anotherList contains this list ID":

An efficient approach is using Join:

var o = from al in anotherList
        join tlId in thelist
        on al.Id equals tlId
        select al;
Community
  • 1
  • 1
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
0
 var o = anotherList.Where(s =>list.Any(a=> a.Id == s.Id));
Akrem
  • 4,903
  • 8
  • 35
  • 61
0

You could also use an anonymous method:

 var o = anotherList.Where(delegate(someItem s) { return list != null && list.Contains(s.ID); });
Mintey
  • 118
  • 11