开发者

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<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 ha开发者_如何学God 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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜