Reputation: 3823
I have a List of orders purchased on specific date. I need to group them all into one date ( purchases made on same day) and then sum them.. I made a class which looks like this:
public class StoreAnalyticOrders
{
public DateTime OrderDate { get; set; }
public float AmountPaid { get; set; }
}
And then the I fill the list which is called "GraphList" with results...
The LINQ that I tried to perform what I just described up there is:
var result = GraphList
.GroupBy(l => l.OrderDate)
.Select(cl => new StoreAnalyticOrders
{
AmountPaid = cl.Sum(c => c.AmountPaid),
}).ToList();
But for some reason the dates are in bad format (they are lost) and they show up like this:
1/1/0001 12:00:00 AM
and this is the previous format of the Order date property:
11/21/2016 6:05:32 AM
What am I doing wrong here?
Edit: @Ivan Stoev is this what you ment:
var result = GraphList
.GroupBy(l => l.OrderDate.Date)
.Select(cl => new StoreAnalyticOrders
{
OrderDate = cl.Key,
AmountPaid = cl.Sum(c => c.AmountPaid)
}).ToList();
Upvotes: 0
Views: 1203
Reputation: 34150
You can not use make changes to datetime in a linq to entities query, and you should use DbFunctions
:
GraphList.GroupBy(gl => DbFunctions.TruncateTime((DateTime)gl.Datetime)).
Select(gl=> new{
Date = DbFunctions.TruncateTime((DateTime)gl.FirstOrDefault().Datetime),
TotalAmountPaid = gl.Sum(x=> x.AmountPaid)
}).ToList();
Upvotes: 1