当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Finding an item based on its enumerable property

问题描述:

I have a List where the Item object has a List inside of it. Can I use the built-in Find of the list to return the Item that has a particular set of Data objects inside of it?

Something like (Psuedo-like code):

users = List<Item>(new Item {name = "Bob, data = new List<Data>()}, new Item {name = "Bill", data = new List<Data>()})

users.Find(i => i.data.key == "foo" && i.data.value == "bar")

网友答案:

You can use linq-expression inside linq-expression. For instance:

users.Find(i => i.data.Any(a => <your condition>))

网友答案:

Find is specific method for List<T>, but IEnumerable<T> has the same extension method, namely FirstOrDefault it's contained in the System.Linq namespace and you can use it like this:

users.FirstOrDefault(i => i.data.key == "foo" && i.data.value == "bar")

But if you are using only List<T> it doesn't really matters what you are going to pick.

分享给朋友:
您可能感兴趣的文章:
随机阅读: