Hello every one. I am trying to group by a date range by week, months.
what is the scenerio here, I am quering a table with Linq to Entities to fetch the records. After that, I have to group that records by weeks or months. say I have query to fetch as
var childCustomerIds= _customerService.GetCustomerByParentId(userId).Select(x=>x.Id).Distinct().ToList();
var surveyResponseList = _productSurveyResponseRepository.Table.Where(x => childCustomerIds.Contains(x.UserID)).ToList();
if (startDate != null)
surveyResponseList = surveyResponseList.Where(x => x.CreatedOnUtc >= startDate).ToList();
if (endDate != null)
surveyResponseList = surveyResponseList.Where(x => x.CreatedOnUtc <= endDate).ToList();
return surveyResponseList;
After that, I have to group this records for weeks or months. If i want to display in weeks format for last month the grouping should be lke this, Week1 => lsit record ; Week2 => list record; Week3 => lsit record ; Week4 => list record;
If I want display record as month grouping like
October => list record; September => list record; August => list record.
imteyazahmad...
Member
286 Points
131 Posts
Group By Week in Linq
Nov 17, 2012 12:32 PM|LINK
Hello every one. I am trying to group by a date range by week, months.
what is the scenerio here, I am quering a table with Linq to Entities to fetch the records. After that, I have to group that records by weeks or months. say I have query to fetch as
var childCustomerIds= _customerService.GetCustomerByParentId(userId).Select(x=>x.Id).Distinct().ToList(); var surveyResponseList = _productSurveyResponseRepository.Table.Where(x => childCustomerIds.Contains(x.UserID)).ToList(); if (startDate != null) surveyResponseList = surveyResponseList.Where(x => x.CreatedOnUtc >= startDate).ToList(); if (endDate != null) surveyResponseList = surveyResponseList.Where(x => x.CreatedOnUtc <= endDate).ToList(); return surveyResponseList;After that, I have to group this records for weeks or months. If i want to display in weeks format for last month the grouping should be lke this, Week1 => lsit record ; Week2 => list record; Week3 => lsit record ; Week4 => list record;
If I want display record as month grouping like
October => list record; September => list record; August => list record.
please, need urgent help on it...
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Group By Week in Linq
Nov 18, 2012 01:07 AM|LINK
Hello,
To group by week, you should first have such a field, and then use linq like this following:
var result = from item in xxxDatacontent
order by item.Week;