I have a datatable which contains the data of whole month. Now i want to get those data group by days via linq. I already have this query but i need a little change. My day starts from 7 O Clock & ends at next day 7 O Clock. My current query is :
var query = dt.AsEnumerable()
.GroupBy(row => new
{
day = row.Field<DateTime>("dateandTime"),
Code = row.Field<string>("Code"),
Name = row.Field<string>("Name")
})
.Select(g => new
{
Name = g.Key.Name,
day = g.Key.day,
Code = g.Key.Code,
quantity = g.Count()
}
);
dateandtime
formate is : 2014-01-02 05:26:10.567
How can i get the data from the datatable group by days of the whole month ?
I want the data from 7 to 7. If the month ends, then last date data should also get the data of next date upto 7 O Clock.
Just group by the Day
property of the DateTime
value, after adjusting the date/time accordingly by 7 hours. Of course, if you actually have multiple months/years involved, that will give you somewhat odd results, but...
.GroupBy(row => new
{
Day = row.Field<DateTime>("dateandTime").AddHours(-7).Day,
Code = row.Field<string>("Code"),
Name = row.Field<string>("Name")
})
(Note: you'll need to make sure that your original query to find the data for a month takes the 7 hour shift into account. Also, if this 7 hour shift is actually due to time zones, there are probably better options.)
See more on this question at Stackoverflow