简体   繁体   中英

Using LINQ, how do I look at List that is contained in another object/List?

Say I have IEnumOfFood that contains a food object that lists out each ingredient. So something like IEnumOfFood[0] would be {'Celery', 'Onion', 'Chicken', [Stock]}. In this case, [Stock] is another IEnumerable. I'm trying to get each element in IEnumOfFood where Stock.Herb == "Bay Leaf". It seems that no matter how I try and reword the LINQ query it always has some sort of error. Currently I have this:

IEnumOfFood= IEnumFoodItems.Select(x => x.Select(
y => y.Stock.Where(
y => y.Herb == "BayLeaf")));

Which throws a 'Cannot convert lambda expression to type 'bool' because it is not a delegate type'. This is just one of the many ways I've tried to get an element of IEnumOfFood where the Stock Enum has herb as 'Bay Leaf', I'm not saying this is what should work.

Have you tryed:

IEnumFoodItems.Where(x => x.Any(s => s.Stock.Any(h => h.Herb == "BayLeaf")))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM