.net c# GroupBy 表达式

GroupBy Expression
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我有以下对象,我希望得到一个结果,该结果提供按第一个类别描述分组的对象集合,然后按子类别描述分组:

Category(Count)
     SubCategory(Count)
     SubCategory(Count)

public class Posting
{
    public Category Category { get; set; }
    public SubCategory SubCategory { get; set; }
}

public class Category
{
    public string Description { get; set; }
}

public class SubCategory
{
    public string Description { get; set; }
}

编辑

所以我有这样的工作:

foreach (var category in col.GroupBy(x => x.Category.Description).Select(x => new { CategoryName = x.Key, Items = x }).OrderBy(x => x.CategoryName))
            {
                Console.WriteLine(category.CategoryName);
                foreach (var subCategory in category.Items.GroupBy(x => x.SubCategory.Description).Select(x => new { SubCategoryName = x.Key }).OrderBy(x => x.SubCategoryName))
                {
                    Console.WriteLine("\t" + subCategory.SubCategoryName);
                }
            }

但我想把它放到一个对象图中,这可能吗?

解决过程1

这似乎非常接近,需要注意Key的是"内部"子类别分组的字段是包含类别和子类别的匿名类型.但是,根据您的应用程序正在执行的操作,这可能是一项功能,因为您将始终拥有可用的类别.

var results = col
    .GroupBy(posting => new
        {
            Category = posting.Category.Description,
            SubCategory = posting.SubCategory.Description
        })
    .GroupBy(group => group.Key.Category.Description);

I have the following object(s) and I want to have a result that provides a collection of objects grouped by first category description and then grouped by subcategory description with:

Category(Count)
     SubCategory(Count)
     SubCategory(Count)

public class Posting
{
    public Category Category { get; set; }
    public SubCategory SubCategory { get; set; }
}

public class Category
{
    public string Description { get; set; }
}

public class SubCategory
{
    public string Description { get; set; }
}

EDIT

So I have this working like so:

foreach (var category in col.GroupBy(x => x.Category.Description).Select(x => new { CategoryName = x.Key, Items = x }).OrderBy(x => x.CategoryName))
            {
                Console.WriteLine(category.CategoryName);
                foreach (var subCategory in category.Items.GroupBy(x => x.SubCategory.Description).Select(x => new { SubCategoryName = x.Key }).OrderBy(x => x.SubCategoryName))
                {
                    Console.WriteLine("\t" + subCategory.SubCategoryName);
                }
            }

But I would like to get this into one object graph, is that possible?

Solutions1

This seems to be pretty close, with the caveat that the Key field of the "inner" sub-category groupings is an anonymous type containing both the Category and SubCategory. But, depending on what your application is doing, that might be a feature since you'll always have the Category available.

var results = col
    .GroupBy(posting => new
        {
            Category = posting.Category.Description,
            SubCategory = posting.SubCategory.Description
        })
    .GroupBy(group => group.Key.Category.Description);
转载于:https://stackoverflow.com/questions/15238793/groupby-expression

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入