简体   繁体   English

分组 <anonymous type:, string> 不包含“”的定义,也没有扩展方法“”

[英]IGrouping<anonymous type:, string> does not contain a definition for '' and no extension method ''

Goal : Group by 2 columns 目标 :按2列分组

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 错误 :这将导致错误IGrouping不包含“ Sub_ID”的定义,并且没有扩展方法“ SubID”接受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. 我也尝试添加.key,但这没有帮助。 Did not find a solution in How to order IGrouping without changing its type? 没有在不更改其类型的情况下如何订购IGrouping找到解决方案 or IGrouping does not contain a definition for IGrouping不包含以下定义

Been looking at this for hours 看着这个已经好几个小时了

You are sorting the groups, and you have to access the grouped property(ies) through the Key : 您正在对组进行排序,并且必须通过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: 编辑:如果只想通过Sub_ID显示不同的元素,请添加.Select(x => x.First())以仅选择每个组的顶部结果:

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> . 使用GroupBy您可以将IQueryable<T>转换为IGrouping<TKey, T> The last Select is to reduce the groupings back to single elements. 最后的Select降低分组回单元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM