Reputation: 95
I have list with 2 parameter's (dynamic)
every list may have a same datetime
and diffrent price
- DateTime -- Price
- 10/10/10 -- 100
- 11/11/11 -- 111
- 11/11/11 -- 100
- 10/10/10 -- 122
- etc
now i need to combine them. for i see only 1 datetime and 1 price
- DateTime -- Price
- 10/10/10 -- 222
- 11/11/11 -- 211
- etc
here the code
var data = db.CheckOut.Where(x => x.ISOrderComplete == true).OrderBy(c => c.Order.OrderDate).ToArray()
.GroupBy(y => new { OrderDate = y.Order.OrderDate, TotalPrice = y.TotalPrice })
.Select(a => new { OrderDate = a.Key.OrderDate, TotalPrice = a.Key.TotalPrice })
.ToList();
I try to add the function the
var data = db.CheckOut.Where(x => x.ISOrderComplete == true).OrderBy(c => c.Order.OrderDate).ToArray()
.GroupBy(y => new { OrderDate = y.Order.OrderDate, TotalPrice = y.TotalPrice })
.Select(a => new { OrderDate = a.Key.OrderDate, TotalPrice = a.Sum(b => b.TotalPrice) })
.ToList();
What i have to do?
i dont need this sum in db. i need this sum to display statistic about incoms to company in charts so i need to sum each data for how much getting .
Upvotes: 0
Views: 584
Reputation: 95
becuse in Linq we cant groupby orderdate.date
so i split it. then we can order by the date
.
public JsonResult TotalIncomeJson()
{
var tempD = db.CheckOut.Where(x => x.ISOrderComplete).ToList();
var data = tempD.GroupBy(x => x.Order.OrderDate.Date).Select(y => new { OrderDate = y.Key, TotalPrice = y.Sum(a => a.TotalPrice) })
.OrderBy(b=>b.OrderDate.Year).ToList();
return Json(data, JsonRequestBehavior.AllowGet);
}
Upvotes: 0
Reputation: 386
Following deramko approach, the only missing thing is that you shouldn't group by OrderDate
, but instead, OrderDate.Date
, because the time can be different.
Try something like this:
var data = db.CheckOut.Where(x => x.IsCheckoutComplete)
.GroupBy(x => new { OrderDate = x.Order.OrderDate.Date})
.Select(a => new { OrderDate = a.Key.OrderDate, TotalPrice = a.Sum(b => b.Order.TotalPrice)})
.OrderBy(c => c.OrderDate)
.ToList();
You can check it on https://dotnetfiddle.net/3mrZkf
Upvotes: 1
Reputation: 2835
var data = db.CheckOut.Where(x => x.ISOrderComplete == true)
.GroupBy(y => new { OrderDate = y.Order.OrderDate})
.Select(a => new { OrderDate = a.Key.OrderDate, TotalPrice = a.Sum(b => b.TotalPrice)})
.OrderBy(c => c.Order.OrderDate)
.ToList();
Upvotes: 1