2

I want Linq to return an array. In order to plot the graph.

Here is my code

var orders = _uow.Orders.GetAll()
            .Where(x => x.Created > baselineDate)
            .GroupBy(x => x.Created.Day)
            .Select(c => new {Day = c.Key, Total = c.Sum(t => t.Total)})
            .ToList();

Now it returns this

[{"Day":3,"Total":9999.00},{"Day":4,"Total":9999.00},{"Day":5,"Total":9999.00}]

BUT I want a result of

{"Day":[3,4,5], "Total":[9999, 9999, 9999]}

this result is easier to plot the graph.

Thank you all

riseres
  • 3,004
  • 4
  • 28
  • 40

2 Answers2

4
var groups = _uow.Orders.GetAll()
            .Where(x => x.Created > baselineDate)
            .GroupBy(x => x.Created.Day);

var orders = new {
    Day = groups.Select(g => g.Key).ToArray(),
    Total = groups.Select(g => g.Sum(t => t.Toal)).ToArray()
};
Lee
  • 142,018
  • 20
  • 234
  • 287
  • Please help me this one [link]http://stackoverflow.com/questions/17476542/linq-group-by-and-then-display-datetime-for-dd-mmm. I can't format string. @Lee – riseres Jul 04 '13 at 18:47
0

I haven't tried, but could be like this:

Orders[] orders = (your query).ToArray();
Learner
  • 3,904
  • 6
  • 29
  • 44