C# 使用 LINQ 从列表中选择项目,其中子项包含来自另一个列表的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17505502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:40:42  来源:igfitidea点击:

Select items from a List where the children contain the items from another List using LINQ

c#linq

提问by Davidson Sousa

I have the following classes:

我有以下课程:

Product:

产品:

public class Product
{
    public string Name { get; set; }
    public List<Category> Categories { get; set; }
}

And Category:

和类别:

public class Category
{
    public string Id { get; set; }
    public string Name { get; set; }
}

And I have the following method:

我有以下方法:

public List<Product> FilterProducts(List<Category> categories)
{
    // filtering code here
}

Question: How can I filter my products using a List<Categories>as parameter?

问题:如何使用List<Categories>as 参数过滤我的产品?

EDIT:One thing I forgot to mention is that if I have 2 categories I should be able to see only the products with the category1ANDcategory2. What I've done so far has returned only products with category1 OR category2. Although inherit IEquatableuse Intersect seems interesting I am comparing with the Id's for now.

编辑:我忘记提及的一件事是,如果我有 2 个类别,我应该只能看到具有category1category2的产品。到目前为止,我所做的仅返回了类别 1 或类别 2 的产品。虽然继承IEquatable使用 Intersect 看起来很有趣,但我现在正在与 Id 进行比较。

采纳答案by nemesv

If you want to return all the products which has allthe provided categories in its Categorieswhich means it selects product where it has category1 AND category2.

如果您想返回包含所有提供类别的所有产品,Categories这意味着它选择具有类别 1 和类别 2 的产品。

Then you need to use the Allwith the combination of Contains:

然后你需要All结合使用Contains

public List<Product> FilterProducts(List<Category> categories)
{
    return products.Where(p => categories.All(c => p.Categories.Contains(c))
                   .ToList();
}

If you want to return all the product where it has it least onecetegory from the provided categories which means it selects product where it has category1 OR category2.

如果您想从提供的类别中返回具有至少一个类别的所有产品,这意味着它选择具有类别 1 或类别 2 的产品。

Then you need to use Any

然后你需要使用 Any

public List<Product> FilterProducts(List<Category> categories)
{
    return products.Where(p => categories.Any(c => p.Categories.Contains(c)
                   .ToList();
}

Please not that if your categoriesobjects are not the same instances which you have in the Categoriesproperty of the product or in your Categoryyou are not overriding the Equalsmethod to use the Idyou may want to compare the Ids instead of the category objects themselves.

请注意,如果您的categories对象与您在Categories产品属性中拥有的实例不同,或者您Category没有覆盖使用Equals方法,Id您可能想要比较Ids 而不是类别对象本身。

So something like:

所以像:

The solution with all

所有的解决方案

public List<Product> FilterProducts(List<Category> categories)
{
     return products.Where(p => categories
        .All(c => p.Categories.Any(cat => cat.Id == c.Id)).ToList()
}

The solution with any

任何解决方案

public List<Product> FilterProducts(List<Category> categories)
{
    return products.Where(p => categories
        .Any(cat => p.Categories.Any(pcat => pcat.Id == cat.Id)).ToList();
}