Reputation: 73
So, i have a piece of code
var _activeContract = (from cnt in UnitOfWork.Context.Contract.Where(w => w.IS_DELETED == 0 && w.CONTRACT_STATUS_ID == (int)NemsKysContractStatusEnum.Aktif)
join sei in UnitOfWork.Context.SiteExtendedInfoList.Where(w => w.IS_DELETED == 0) on cnt.SITE_ID equals sei.SITE_ID
join sr in UnitOfWork.Context.Subregion.Where(w => w.IS_DELETED == 0) on sei.RN_SUBREGION_ID equals sr.ID
join r in UnitOfWork.Context.Region.Where(w => w.IS_DELETED == 0) on sr.REGION_ID equals r.ID
join mr in UnitOfWork.Context.MainRegion.Where(w => w.IS_DELETED == 0) on r.MAIN_REGION_ID equals mr.ID
select new { MRNAME = mr.NAME, cnt.ID }).GroupBy(g => g.MRNAME).OrderBy(t => t.Key).Select(s => new { name = s.Key, data = s.Count() }).ToList();
I can't understand the last line.
As I know it says to create the output sequence, each element of it is
{ MRNAME = mr.NAME, cnt.ID }
i.e. anon object. Then we group this sequence by MRNAME
and then we order it by Key
(part OrderBy(t=>t.Key)
). What Key
? Where is this key from? Because we just have two fields: MRNAME
and cntID
, don't we?
Then I can't understand how
Select(s => new { name = s.Key, data = s.Count() })
works. It creates on each element of previous sequence of { MRNAME = mr.NAME, cnt.ID }
objects a new sequence each element of that is an object like { name = s.Key, data = s.Count() }
.
Again i don't understand what Key
is here and what we count calculate field data by s.Count()
, what we count exactly?
Upvotes: 0
Views: 76
Reputation: 110081
from ...
select new { MRNAME = mr.NAME, cnt.ID }) //1
.GroupBy(g => g.MRNAME) //2
.OrderBy(t => t.Key) //3
.Select(s => new { name = s.Key, data = s.Count() }) //4
.ToList();
Each element of the output from GroupBy is an IGrouping, which has a Key property.
Reading the code call by call:
//1 Each element has MRName and ID properties.
//2 Group the elements by the MRName property, we now have a query consisting of groups.
//3 Order the groups by the Key, which is the MRName property.
//4 Turn each group into a new element. Each element has a name and data property.
Upvotes: 1