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
  • 17,013
  • 3
  • 59
  • 105
Jack Manson
  • 497
  • 1
  • 7
  • 9
  • 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
  • 44,454
  • 10
  • 85
  • 129
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
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
  • 450,073
  • 74
  • 686
  • 939
0
 var o = anotherList.Where(s =>list.Any(a=> a.Id == s.Id));
Akrem
  • 5,033
  • 8
  • 37
  • 64
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