How can I use .Contains() to get at a property of a collection using LINQ to Objects?
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
, 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.<Recipient
> Recipients
If I just ha开发者_如何学God an IEnumerable
, it would be easy to do <Int
> RecipientsRecipients.Contains(5);
But, because I'm trying to get a property of the collection, how do I use Contains in that instance?
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)
精彩评论