如何在没有 ForEach 的情况下从嵌套集合中进行过滤?
我有一个实体,类型 A 的属性类型为 B 的列表。类型 B 也具有类型为 C 的列表的属性。
我想对 A 的对象应用过滤器,这样在 C 的列表中就只有 C 对象他们的 Selected 属性是 True。
这可以这样做:
A objA = A.ListB.ForEach(b => {b.ListC.RemoveAll(c => c.Selected == false);});
但我不必删除所有那些 Selected = false 的 C 对象。我只想过滤它们。
有什么想法吗?
更多说明:有一个A类型的对象,具有B属性的List。在 A 的 B 的 List 的每个 B 对象中,都存在一个 C 的 List 属性。 C 对象有一个选定的属性。现在,我需要的是 - 一个带有 B 列表的 A 对象,其中每个 B 的 C 列表中只有那些 Selected = true 的 C 对象。 所需的输出是类型 A。不应过滤列表 B,仅需要过滤列表 C。
I have an entity say Type A with property type List of B. Type B also has a property of type List of C.
I want to apply the filter on object of A such that there would be only C objects in the List of C for which their Selected property is True.
This can be done like:
A objA = A.ListB.ForEach(b => {b.ListC.RemoveAll(c => c.Selected == false);});
But I don't have to remove all those C Objects which have Selected = false. I only want to filter them.
Any ideas?
More explanation: There is an object of Type A, with List of B property. In each B object of A's List of B, there exists a List of C property.
C object has a Selected Property. Now, all I need is- an object of A with List of B, where in each of B's List of C has only those C objects which have Selected = true.
The desirable output is type A. List B shouldn't be filtered only List C needs to be filtered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
这是你想要的吗?
What about this:
Is this what you want?
如果您想要一个包含所有选定 C 对象的列表,您可以这样做:
If you want a list containing all the selected C objects, you can do this:
请注意,这只是一个匿名元组;如果没有有关代码结构、有哪些 props 等的更多信息,我们无法显示重建
A
/B
等的代码note that this is just an anonymous tuple; we can't show code to reconstruct an
A
/B
etc without more info on how your code is structured, what props there are, etc