1

I have code like this:

.Where(o => o.Parents.Contains(name))

The above code does not work because Parents contain a list of Parent objects, which in turn have a property Name. I want to check against the Name property, but it's a list so how could I do this check? So I want the Where to be true when any of the Parent objects in the list have Name property set to name.

4

3 回答 3

2

There's a simple fix for this: use more LINQ.

.Where(o => o.Parents.Any(p => p.Name == name))

As an alternative, you could use the slightly more verbose (but equally lazy)

.Where(o => o.Parents.Select(p => p.Name).Contains(name))
于 2012-05-10T20:07:37.157 回答
2

Try the following code snippet:

.Where(o => o.Parents.Any(p => p.Name == name))
于 2012-05-10T20:07:58.507 回答
0

您可以使用 .Any 在要检查特定属性的对象集合中检查特定条件。

.Where(o => o.Childs.Any(child => child.Name == o.Name));
于 2012-05-10T20:10:57.147 回答