C# Linq按特定字段分组,然后转换为字典

C# Linq按特定字段分组,然后转换为字典,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我有一个有一些评级细节的平面收藏 public class testClass { public string Label { get; set; } public byte Rating { get; set; } public Int32 ReviewByRatingCount { get; set; } public Int32 TotalReviewsByRating { get; set; } public double AverageRatin

我有一个有一些评级细节的平面收藏

public class testClass
{
    public string Label { get; set; }
    public byte Rating { get; set; }
    public Int32 ReviewByRatingCount { get; set; }
    public Int32 TotalReviewsByRating { get; set; }
    public double AverageRating { get; set; }

}

var list = new List<testClass>
{
    new testClass { Label = "Model 3", TotalReviewsByRating = 38, AverageRating= 4.58, Rating = 5, ReviewByRatingCount =30},
    new testClass { Label = "Model 3", TotalReviewsByRating = 38, AverageRating= 4.58, Rating = 4, ReviewByRatingCount =5},
    new testClass { Label = "Model 3", TotalReviewsByRating = 38, AverageRating= 4.58, Rating = 1, ReviewByRatingCount =2},
    new testClass { Label = "Model 3", TotalReviewsByRating = 38, AverageRating= 4.58, Rating = 2, ReviewByRatingCount =1}
};
我已经实现了使用multiplegroupby和select语句作为键值对进行转换。但我想知道,是否有更好的方法

public class testClassDto
{
    public string Label { get; set; }
    public Dictionary<string,int> ReviewsByRatingCount { get; set; }
    public Int32 TotalReviewsByRating { get; set; }
    public double AverageRating { get; set; }

}

var data = list.GroupBy(p => new { p.Label , p.TotalReviewsByRating, p.AverageRating})
    .Select(group => new testClassDto 
    { 
        Label = group.Key.Label,                                                     
        TotalReviewsByRating = group.Key.TotalReviewsByRating,
        AverageRating = group.Key.AverageRating,                                     
        ReviewsByRatingCount = group.Select(s => new {s.Rating,s.ReviewByRatingCount})
            .ToDictionary(s => s.Rating.ToString(),s => s.ReviewByRatingCount)
    }); 
公共类testClassDto
{
公共字符串标签{get;set;}
公共词典ReviewByRatingCount{get;set;}
公共Int32 TotalReviewByRating{get;set;}
公共双平均{get;set;}
}
var data=list.GroupBy(p=>new{p.Label,p.totalReviewByRating,p.AverageRating})
.选择(组=>new testClassDto
{ 
Label=group.Key.Label,
TotalReviewByRating=group.Key.TotalReviewByRating,
AverageRating=group.Key.AverageRating,
ReviewByRatingCount=组。选择(s=>new{s.Rating,s.ReviewByRatingCount})
.ToDictionary(s=>s.Rating.ToString(),s=>s.ReviewByRatingCount)
}); 

更好的方法?代码的哪一部分似乎不令人满意,或者您考虑了哪些可能的改进?我看到的是,当您通过执行
ReviewByRatingCount=group.ToDictionary(s=>s.Rating.ToString(),s=>s.ReviewByRatingCount)来分配
ReviewByRatingCount
的值时,您可以跳过
.Select()
语句。
目前,我在源集合中有很多文件,但为了简洁起见,我删除了这些文件。因为我不喜欢创建任何大约有12个文件的类型。上面的代码我只添加了三个字段。在生成字典时,顺序应该由值字段(Decenting)定义。但它对我的关键字字段进行排序。@Arooran字典没有保证的顺序。如果您需要特定顺序的数据,那么您需要的不是字典。是否使用有序字典?更好的方法?代码的哪一部分似乎不令人满意,或者您考虑了哪些可能的改进?我看到的是,当您通过执行
ReviewByRatingCount=group.ToDictionary(s=>s.Rating.ToString(),s=>s.ReviewByRatingCount)来分配
ReviewByRatingCount
的值时,您可以跳过
.Select()
语句。
目前,我在源集合中有很多文件,但为了简洁起见,我删除了这些文件。因为我不喜欢创建任何大约有12个文件的类型。上面的代码我只添加了三个字段。在生成字典时,顺序应该由值字段(Decenting)定义。但它对我的关键字字段进行排序。@Arooran字典没有保证的顺序。如果您需要特定顺序的数据,那么您需要的不是字典。是否使用有序字典?
public class testClassDto
{
    public string Label { get; set; }
    public Dictionary<string,int> ReviewsByRatingCount { get; set; }
    public Int32 TotalReviewsByRating { get; set; }
    public double AverageRating { get; set; }

}

var data = list.GroupBy(p => new { p.Label , p.TotalReviewsByRating, p.AverageRating})
    .Select(group => new testClassDto 
    { 
        Label = group.Key.Label,                                                     
        TotalReviewsByRating = group.Key.TotalReviewsByRating,
        AverageRating = group.Key.AverageRating,                                     
        ReviewsByRatingCount = group.Select(s => new {s.Rating,s.ReviewByRatingCount})
            .ToDictionary(s => s.Rating.ToString(),s => s.ReviewByRatingCount)
    });