【问题标题】:Use GroupBy to make SubLists from List where the GroupBy value is a List<t>使用 GroupBy 从 List 中创建子列表,其中 GroupBy 值为 List<t>
【发布时间】:2021-11-01 07:26:43
【问题描述】:

我有一个想要显示的简单列表,按它所属的类别分组。我见过的所有示例都使用 GroupBy 但只有一个 ID,我无法弄清楚如何使用 List 来执行此操作。如果产品出现在两个类别下就可以了。

public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }
    public List<Category> Categories { get; set; }
}

StringBuilder ProductList = new StringBuilder();
var p = _products.GroupBy(a => a.Categories);

foreach (var item in p)
{
    ProductList.Append($"<p><strong>{item.Key}</strong><br/>");
    foreach (var e in item)
    {
        ProductList.Append($"{e.Title}");
        ProductList.Append("</p>");
    }
}

【问题讨论】:

  • 您想显示每个类别或一组特定类别的值?

标签: c# linq group-by


【解决方案1】:

这是一个包含示例数据的解决方案,您可以自己测试:

        var categories = Enumerable.Range(0, 10).Select(n => new Category { Id = n }).ToArray();
        var products = new[]
        {
            new Product { Id = 1, Title = "P1", Categories = new List<Category> { categories[0], categories[1], categories[2] } },
            new Product { Id = 2, Title = "P2", Categories = new List<Category> { categories[0], categories[1], categories[3] } },
            new Product { Id = 3, Title = "P3", Categories = new List<Category> { categories[0], categories[2], categories[3] } },
            new Product { Id = 4, Title = "P4", Categories = new List<Category> { categories[0], categories[2], categories[3] } },
            new Product { Id = 5, Title = "P5", Categories = new List<Category> { categories[0], categories[3], categories[5] } },
            new Product { Id = 6, Title = "P6", Categories = new List<Category> { categories[0], categories[4], categories[5] } },
        };

        var categoryGroups =
            from p in products
            from c in p.Categories
            group p by c.Id into g
            select g;

        foreach (var categoryGroup in categoryGroups)
        {
            Console.WriteLine($"Category {categoryGroup.Key}:");
            
            foreach (var product in categoryGroup)
            {
                Console.WriteLine($"\tProduct {product.Id}: {product.Title}");
            }

            Console.WriteLine("-----------------------------------------------------");
        }

我假设 Category 类有一些 id 属性,例如Id,分组。如果是基于引用的分组,您可以将 group p by c.Id into g 替换为 group p by c into g

【讨论】:

    【解决方案2】:

    您可以使用SelectMany 将列表扁平化为(Product, Category) 的元组,然后使用GroupBy 按类别对产品进行分组。

    StringBuilder ProductList = new StringBuilder();
    var p = _products
        .SelectMany(a => a.Categories, (prod, cat) => (prod, cat))
        .GroupBy(tuple => tuple.cat, tuple => tuple.prod);
    
    foreach (var item in p)
    {
        ProductList.Append($"<p><strong>{item.Key}</strong><br/>");
        foreach (var e in item)
        {
            ProductList.Append(e.Title);
            ProductList.Append("</p>");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2010-09-26
      • 1970-01-01
      • 2019-05-19
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2022-12-05
      相关资源
      最近更新 更多