我有以下对象,并且我希望有一个结果,它提供了一个按第一个类别描述分组的对象集合,然后按照子类别描述进行分组:
类别(计数)
.Count=‘Count 2’>亚类(计数)
.Count=‘Count 2’>亚类(计数)
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);
}
}但是我想把它放到一个对象图中,这有可能吗?
发布于 2013-03-06 07:59:42
这似乎相当接近,但要注意的是,“内部”子类别组的Key字段是包含SubCategory和类别的匿名类型。但是,取决于您的应用程序正在做什么,这可能是一个功能,因为您将始终有可用的类别。
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
复制相似问题