I have have a List below, and I am trying to combine the T.name property and sum up the T.score property
Data
Name | score
-------------
Jon | 50
Jon | 100
Ash | 100
Ash | 75
Desired result is
Jon | 150
Ash | 175
I am able to do this in LINQ and create a var with the desired results
List<PieSeriesData> PiSeriesData = new List<PieSeriesData>();
var x = PiSeriesData.GroupBy(i => i.Name)
.Select(g => new { Id = g.Key, Total = g.Sum(i => i.Score) })
.ToList();
The issue with that is I need x to be a List type, which is what the HighCharts nuget requires.
Why not create new class
public class PieSeriesRenderData
{
public string ID { get; set; }
public int Total { get; set; }
}
And after that return this:
List<PieSeriesData> piSeriesData = new List<PieSeriesData>();
var x = piSeriesData.GroupBy(i => i.Name)
.Select(g => new PieSeriesRenderData { Id = g.Key, Total = g.Sum(i => i.Score) })
.ToList();
Now you will have object of List<PieSeriesRenderData>
which you can return it as Json from your web service and you will render the highchart in your ajax.successful method.
As I understand from the chat you need the output to be a List<PieSeriesData>
. You need to project a new object of PieSeriesData
class in the select
and not of an anonymous type: _See that you also need to assign the properties that exist and not use new ones like Id
and Total
:
List<PieSeriesData> PiSeriesData = new List<PieSeriesData>();
var x = PiSeriesData.GroupBy(i => i.Name)
.Select(g => new PieSeriesData {
Name = g.Key,
Score = g.Sum(i => i.Score)
}).ToList();
// Now x is of type: List<PieSeriesData>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.