views:

27

answers:

1

I have an IEnumerable of the following object:

public class Recipient  
{  
    public  int UserID { get; set; }
    public  string Name { get; set; }
}

So, I have an IEnumerable<Recipient> Recipients, and I'd like to do a .Contains() on Recipients. Except, I'd like to do a .contains() on each recipients UserID, to see if my Recipients contain a particular userID.

If I just had an IEnumerable<Int> Recipients, it would be easy to do Recipients.Contains(5);

But, because I'm trying to get a property of the collection, how do I use Contains in that instance?

+2  A: 
Recipients.Any(r => r.UserID == 5)

Alternatively, you can map the collection to a collection of UserID values and perform a Contains there:

Recipients.Select(r => r.UserID).Contains(5)
Mehrdad Afshari