我有这样的代码:
.Where(o => o.Parents.Contains(name))
上述代码不起作用,因为Parents
包含Parent
个对象的列表,而这些对象又具有属性Name
。我想检查Name
属性,但它是一个列表,所以我该怎么做这个检查?因此,当列表中的任何Where
对象将true
属性设置为Parent
时,我希望Name
为name
。
答案 0 :(得分:2)
对此有一个简单的解决方法:使用更多LINQ。
.Where(o => o.Parents.Any(p => p.Name == name))
作为替代方案,您可以使用稍微冗长(但同样懒惰)的
.Where(o => o.Parents.Select(p => p.Name).Contains(name))
答案 1 :(得分:2)
请尝试以下代码段:
.Where(o => o.Parents.Any(p => p.Name == name))
答案 2 :(得分:0)
您可以使用.Any检查要检查特定属性的对象集合中的特定条件。
.Where(o => o.Childs.Any(child => child.Name == o.Name));