问题描述:

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.

相关阅读:
Top