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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.