C# 使用LINQ生成Highchart列表数据

C# 使用LINQ生成Highchart列表数据,c#,linq,highcharts,C#,Linq,Highcharts,我有一个文章数据列表list 输入数据 List<Article> input = new List<Article> { new Article { Author = "author1", ReadingNumber = 49, LikeNumber = 55, ForwardingNumber = 21 }, new Article { Author = "author1&q

我有一个文章数据列表
list

输入数据

List<Article> input = new List<Article>
        {
            new Article { Author = "author1", ReadingNumber = 49, LikeNumber = 55, ForwardingNumber = 21 },
            new Article { Author = "author1", ReadingNumber = 11, LikeNumber = 35, ForwardingNumber = 79 },
            new Article { Author = "author2", ReadingNumber = 25, LikeNumber = 77, ForwardingNumber = 39 },
        };
我已经厌倦了使用linq和Foreach方法来生成数据:

var authorData=
    (from a in articleList
     group a by a.Author into g
     select new {
       Author = g.Key,
       TotalReading = g.Sum(a=>a.ReadingNumber),
       TotalLikes = g.Sum(a=>a.LikeNumber),
       .......
    }).ToList()
然后我使用
ForEach
方法来获取我需要的每个属性

               var totalReading = new AuthorData()
                {
                    name = "TotalReading",
                    data = new List<string>()
                };
                authorData.ForEach(a => totalRead.data.Add(a.TotalRead.ToString()));
var totalReading=new AuthorData()
{
name=“TotalReading”,
数据=新列表()
};
authorData.ForEach(a=>totalRead.data.Add(a.totalRead.ToString());
最后我把它们组合成结果

var result =
 {
    Authors=authorData.Select(a=>a.Author).ToList(),
    series = new List<AuthorData>{totalReading,totalLike,totalForwrad.....}
}
var结果=
{
Authors=authorData.Select(a=>a.Author.ToList(),
series=新列表{totalReading、TotalIke、totalForwrad….}
}

我觉得很复杂。如何简化此解决方案?

不幸的是,您没有指定您的输入是什么以及为什么它应该看起来像预期的输出。但我想这个解决方案应该可以建立您的预期输出。我使用反射来构建它。如果添加或删除任何整数属性,它们将自动添加或从输出字符串中删除

编辑:在你编辑了你的问题之后,我发现你需要计算财产的总和。这在你最初的问题中并不清楚。这是更新的代码:

List<Article> input = new List<Article>
{
    new Article { Author = "author1", ReadingNumber = 49, LikeNumber = 55, ForwardingNumber = 74 },
    new Article { Author = "author1", ReadingNumber = 141, LikeNumber = 78, ForwardingNumber = 38 },
    new Article { Author = "author2", ReadingNumber = 106, LikeNumber = 38, ForwardingNumber = 39 },
};
        
var grouped = input.GroupBy(x => x.Author).Select(x => new
{
    Author = x.Key,
    TotalReadingNumber = x.Sum(y=>y.ReadingNumber),
    TotalLikeNumber = x.Sum(y=>y.LikeNumber),
    TotalForwardingNumber = x.Sum(y=>y.ForwardingNumber),
}).ToList();
    
StringBuilder output = new StringBuilder();
output.AppendLine("xAxis: {");
output.AppendLine("categories: [");
output.AppendLine(string.Join(",", input.Select(x => $"'{x.Author}'")));
output.AppendLine("]");
output.AppendLine("},"); 
output.Append("series: [");
bool first = true;
foreach(var property in grouped.GetType().GetGenericArguments().First().GetProperties().Where(x => x.PropertyType == typeof(int)))
{
    if(!first)
    {
        output.Append(", ");
    }
    output.AppendLine("{");
    output.AppendLine("name: 'Total" + property.Name + "',");
    output.AppendLine("data: [" + string.Join(",", grouped.Select(x => property.GetValue(x))) + "]");
    output.Append("}");
    first = false;
}
output.AppendLine("]");

List

这是一个很有前途的解决方案,但我想将结果作为Json字符串保存到数据库中,这样我就可以在另一个控制器中读取它并将其返回到前端。你能不能检查一下我更新的代码,看看使用反射是否能得到类似的结果。我更新了答案。现在它应该与您的问题相匹配了。您好@ichPotatodeCXY,您使用了highcharts标记,但问题似乎不是关于图表,而是关于数据。如果您对Highcharts有问题,请提供一个带有硬编码示例数据的演示。
var result =
 {
    Authors=authorData.Select(a=>a.Author).ToList(),
    series = new List<AuthorData>{totalReading,totalLike,totalForwrad.....}
}
List<Article> input = new List<Article>
{
    new Article { Author = "author1", ReadingNumber = 49, LikeNumber = 55, ForwardingNumber = 74 },
    new Article { Author = "author1", ReadingNumber = 141, LikeNumber = 78, ForwardingNumber = 38 },
    new Article { Author = "author2", ReadingNumber = 106, LikeNumber = 38, ForwardingNumber = 39 },
};
        
var grouped = input.GroupBy(x => x.Author).Select(x => new
{
    Author = x.Key,
    TotalReadingNumber = x.Sum(y=>y.ReadingNumber),
    TotalLikeNumber = x.Sum(y=>y.LikeNumber),
    TotalForwardingNumber = x.Sum(y=>y.ForwardingNumber),
}).ToList();
    
StringBuilder output = new StringBuilder();
output.AppendLine("xAxis: {");
output.AppendLine("categories: [");
output.AppendLine(string.Join(",", input.Select(x => $"'{x.Author}'")));
output.AppendLine("]");
output.AppendLine("},"); 
output.Append("series: [");
bool first = true;
foreach(var property in grouped.GetType().GetGenericArguments().First().GetProperties().Where(x => x.PropertyType == typeof(int)))
{
    if(!first)
    {
        output.Append(", ");
    }
    output.AppendLine("{");
    output.AppendLine("name: 'Total" + property.Name + "',");
    output.AppendLine("data: [" + string.Join(",", grouped.Select(x => property.GetValue(x))) + "]");
    output.Append("}");
    first = false;
}
output.AppendLine("]");