过滤列表基于 T 的每个实例包含的另一个列表

发布于 2024-12-19 17:38:09 字数 702 浏览 4 评论 0原文

因此,我在一个列表中有一组对象,但该列表中的每个对象都包含另一个列表。

请考虑以下事项:

class Parent
{
    public Parent(string parentName)
    {
        this.ParentName = parentName;
    }
    public string ParentName { get; set; }
    public List<Child> Children { get; set; } 
}
class Child
{
    public Child(string name)
    {
        this.ChildName = name;
    }

    public string ChildName { get; set; }
}

根据应用程序的性质,父对象列表中的所有 Parent 对象都是唯一的。多个父母可以包含同一个孩子,我需要获取包含孩子 x 的父母。

因此,假设 ChildName 为“child1”的孩子同时属于 ParentName 为“parent1”和“parent5”的父母。如果集合中有 100 个父母,我只想获取 ChildName 为“child1”的 Child 的父母,

我更愿意使用 lambda 表达式来执行此操作,但我不知道从哪里开始,因为我不知道确实有很多使用它们的经验。是否可能,如果可以,正确的方法是什么?

So I have a collection of objects in one list, but each object in that list contains another list.

Consider the following:

class Parent
{
    public Parent(string parentName)
    {
        this.ParentName = parentName;
    }
    public string ParentName { get; set; }
    public List<Child> Children { get; set; } 
}
class Child
{
    public Child(string name)
    {
        this.ChildName = name;
    }

    public string ChildName { get; set; }
}

By the nature of the application, all Parent objects in the list of parents are unique. Multiple parents can contain the same child, and I need to get the parents that contain child x.

So, say the child with ChildName of "child1" belongs to both parents with ParentName of "parent1" and "parent5". If there are 100 parents in the collection, I want to get only the ones that have the Child with ChildName of "child1"

I would prefer to do this with a lambda expression but I'm not sure where to start as I don't really have to much experience using them. Is it possible, and if so, what is the correct approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-12-26 17:38:09

如果 Child 类通过实现 IEquatable 定义了相等运算,则可以使用 lambda(Enumerable.Where)轻松完成此操作> LINQ 方法和 List.Contains 方法:

var parents = new List<Parent> { ... }; // fully populated list of parents
var child = null; // the child you are looking for goes here
var filtered = parents.Where(p => p.Children.Contains(child));

您现在可以迭代 filtered 并执行业务逻辑。

如果 Child 类没有显式定义的相等操作(这意味着它将使用引用相等规则而不是检查相同的 ChildName),那么您需要包含自己检查 lambda 中的“什么通过等于”:

var filtered = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));

注意:当然还有许多其他方法可以执行上述操作,包括可能更容易阅读的方法

parents.Where(p => p.Children.Count(c => c.ChildName == "child1") > 0);

但是,这效率不高作为 Any 版本,即使它会产生相同的结果。

在这两种情况下,lambda 非常“读起来像”它们想要做的事情:

  1. 我想要那些 Children 集合包含此项目的父母
  2. 我想要那些至少有一个 Children 的父母有 ChildName == "child1"

If the Child class has defined an equality operation by implementing IEquatable<Child>, you can do this easily by using a lambda, the Enumerable.Where method of LINQ and the List.Contains method:

var parents = new List<Parent> { ... }; // fully populated list of parents
var child = null; // the child you are looking for goes here
var filtered = parents.Where(p => p.Children.Contains(child));

You can now iterate over filtered and perform your business logic.

If the Child class does not have an equality operation explicitly defined (which means that it will use reference equality rules instead of checking for identical ChildName), then you would need to include the "what passes for equal" check into the lambda yourself:

var filtered = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));

Note: There are of course many other ways to do the above, including the possibly easier to read

parents.Where(p => p.Children.Count(c => c.ChildName == "child1") > 0);

However, this is not as efficient as the Any version even though it will produce the same results.

In both cases, the lambdas very much "read like" what they are intended to do:

  1. I want those parents where the Children collection contains this item
  2. I want those parents where at least one of the Children has ChildName == "child1"
我ぃ本無心為│何有愛 2024-12-26 17:38:09

你可以这样做:

var result = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));

You can do it like this:

var result = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));
百思不得你姐 2024-12-26 17:38:09

这样就可以了

IEnumerable<Parent> parentsWithChild1 = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));

This would do it

IEnumerable<Parent> parentsWithChild1 = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文