所以,我有一段代码
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();
我无法理解最后一行。
据我所知,创建输出序列时,它的每个元素都是
{ MRNAME = mr.NAME, cnt.ID }
即非对象。然后我们按MRNAME
对此序列进行分组,然后按Key
(部分OrderBy(t=>t.Key)
)对其进行排序。 Key
是什么?这把钥匙来自哪里?因为我们只有两个字段:MRNAME
和cntID
,不是吗?
然后我无法理解如何
Select(s => new { name = s.Key, data = s.Count() })
有效。它在前一个{ MRNAME = mr.NAME, cnt.ID }
个对象序列的每个元素上创建一个新序列,其中每个元素都是{ name = s.Key, data = s.Count() }
之类的对象。
我再次理解Key
在这里是什么以及我们计算的是s.Count()
计算字段数据,我们究竟计算了什么?
答案 0 :(得分:1)
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();
GroupBy输出的每个元素都是一个IGrouping,它具有Key属性。
通过调用读取代码调用:
//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.