我有以下对象,我想要一个结果,提供按第一类别描述分组的对象集合,然后按子类别描述分组:
类别(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);
}
}
但是我想把它变成一个对象图,这可能吗?
答案 0 :(得分:1)
这似乎非常接近,但需要注意的是,“内部”子类别分组的Key
字段是包含Category和SubCategory的匿名类型。但是,根据您的应用程序正在执行的操作,这可能是一项功能,因为您始终可以使用类别。
var results = col
.GroupBy(posting => new
{
Category = posting.Category.Description,
SubCategory = posting.SubCategory.Description
})
.GroupBy(group => group.Key.Category.Description);