简体   繁体   English

使用 LINQ,如何查看包含在另一个对象/列表中的列表?

[英]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.假设我有 IEnumOfFood,其中包含列出每种成分的食物 object。 So something like IEnumOfFood[0] would be {'Celery', 'Onion', 'Chicken', [Stock]}.所以像 IEnumOfFood[0] 这样的东西会是 {'Celery', 'Onion', 'Chicken', [Stock]}。 In this case, [Stock] is another IEnumerable.在这种情况下,[Stock] 是另一个 IEnumerable。 I'm trying to get each element in IEnumOfFood where Stock.Herb == "Bay Leaf".我试图在 IEnumOfFood 中获取 Stock.Herb == "Bay Leaf" 中的每个元素。 It seems that no matter how I try and reword the LINQ query it always has some sort of error.似乎无论我如何尝试改写 LINQ 查询,它总是会出现某种错误。 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'.这会引发“无法将 lambda 表达式转换为类型 'bool',因为它不是委托类型”。 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.这只是我尝试获取 IEnumOfFood 元素的众多方法之一,其中 Stock Enum 的草本植物为“月桂叶”,我并不是说这应该有效。

Have you tryed:你有没有试过:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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