Goal : Group by 2 columns
Error : This causes the error IGrouping does not contain a definition for 'Sub_ID' and no extension method 'SubID' accepting a first argument of type IGrouping
Code :
return db.View.Where(m => m.Mem_ID == Some_MemID && m.Last_Name.ToLower() == Search_MemLastName.ToLower()).GroupBy(t => new { t.Sub_ID, t.Mem_Seq }).OrderBy(t => t.Subs_ID);
I've also tried adding .key but that did not help. Did not find a solution in How to order IGrouping without changing its type? or IGrouping does not contain a definition for
Been looking at this for hours
You are sorting the groups, and you have to access the grouped property(ies) through the Key
:
.OrderBy(t => t.Key.Subs_ID)
Edit: If you only want to display distinct elements by Sub_ID
, add .Select(x => x.First()) to select only the top result per group:
return db.View
.Where(m => m.Mem_ID == Some_MemID && m.Last_Name.ToLower() == Search_MemLastName.ToLower())
.GroupBy(t => new { t.Sub_ID, t.Mem_Seq })
.OrderBy(g => g.Key.Subs_ID)
.Select(g => g.First());
With GroupBy
you transformed an IQueryable<T>
into an IGrouping<TKey, T>
. The last Select
is to reduce the groupings back to single elements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.