I have the following list:
public class Prices
{
public int Year { get; set; }
public decimal Price { get; set; }
}
List<Prices> List1 = new List<Prices>();
List1.Add(new Prices { Year = 2018, Price = 1.5m });
List1.Add(new Prices { Year = 2017, Price = 1.2m });
List1.Add(new Prices { Year = 2016, Price = 3.5m });
//The linq query i'm trying to build
var data1 = List1.Select(c=> new
{
labels = new List<Int64> {c.Year },
series = new List<decimal> {
new List<decimal> { c.Price }
}
});
// it's what i want to build
var data2 = new {
labels = new List<Int64> { 2018, 2017, 2016 },
series = new List<decimal[]> {
new decimal[] { 1.5m, 1.2m, 3.5m }
}
};
var Json = JsonConvert.SerializeObject(data2);
//Json => {"labels":[2018,2017,2016],"series":[[1.5,1.2,3.5]]}
I wish to convert the List1 to a Json Output using linq:
{"labels":[2018,2017,2016],"series":[[1.5,1.2,3.5]]}