开发者

How can I use .Contains() to get at a property of a collection using LINQ to Objects?

开发者 https://www.devze.com 2022-12-14 13:13 出处:网络
I have an IEnumerable of the following object: public class Recipient { publicint UserID { get; set; } publicstring Name { get; set; }

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

精彩评论

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