[英]LINQ: No overload for method 'GroupBy' takes 6 arguments / IGrouping<t,t> does not contain a definition
Problem: Getting error 问题:出现错误
"No overload for method 'GroupBy' takes 6 arguments"
“方法'GroupBy'的重载不接受6个参数”
A lot of the SO articles are for specific user created methods. 许多SO文章都是针对特定用户创建的方法的。 GroupBy is part of the library.
GroupBy是该库的一部分。
I've tried varying the number of arguments. 我尝试过改变参数的数量。 If I change it to 2 arguments, then the errors points to the next area where it has OrderByDescending and gives the error:
如果我将其更改为2个参数,则错误指向具有OrderByDescending的下一个区域并给出错误:
"IGrouping does not contain a definition for 'Start_Date' and no extension method 'Start_Date' accepting a first argument of type 'IGrouping' could be found."
“ IGrouping不包含'Start_Date'的定义,找不到可以接受类型为'IGrouping'的第一个参数的扩展方法'Start_Date'。”
The code that gives this error is: 给出此错误的代码是:
var someVariable = DbContextObject.View.Where(
m =>
m.Some_ID == CurrentlyEditingSomeID
).GroupBy(d=> d.Start_Date,f=>f.Some_ID).AsQueryable().OrderByDescending(m => m.Start_Date);
To be used in a ListView 在ListView中使用
You need to create anonymous object with list of all fields to be included in group by, then access those fields using Key property of grouped list, something like below - 您需要创建一个匿名对象,其中包含要包含在group by中的所有字段的列表,然后使用分组列表的Key属性访问这些字段,如下所示-
var someVariable = DbContextObject.View.Where(
m =>
m.Some_ID == CurrentlyEditingSomeID
).GroupBy(d=> new { d.Start_Date,d.Some_ID}).AsQueryable().OrderByDescending(m => m.Key.Start_Date);
So the input of your GroupBy
is a sequence of Views
. 因此,
GroupBy
的输入是一系列Views
。 Every View
has at least a StartDate
and SomeId
. 每个
View
至少都有一个StartDate
和SomeId
。
Your GroupBy
groups all input Views
into groups of of items extracted from the Views
with the same StartDate
. 您的
GroupBy
将所有输入的Views
分组为从具有相同StartDate
的Views
提取的项目组。 Every Group has a Key
containing this common StartDate
, the elements in the group are the SomeId
of the Views
in the group. 每个组都有一个包含此公共
StartDate
的Key
,该组中的元素是该组中Views
的SomeId
。
The result of the GroupBy
is already IQueryable<...>
. GroupBy
的结果已经是IQueryable<...>
。 So AsQueryable
is unnecesary, it will only slow down your process. 因此,
AsQueryable
是不必要的,只会减慢您的处理速度。
The input of your Orderby
is a sequence of groups. 您的
Orderby
的输入是一组序列。 Alas, groups don't have a StartDate
. groups,群组没有
StartDate
。 Luckily, groups have a Key
containing the StartDate
that you want to order by. 幸运的是,组具有包含您要作为排序依据的
StartDate
的Key
。
var result = DbContextObject.Views
// I onlly want the views with a certain SomeId:
.Where(view => view.SomeID == CurrentlyEditingSomeID)
// group the views into groups with same StartDate
// the elements in the group are the SomeId
.GroupBy(view => view.StartDate, view=>view.SomeID)
// result: a sequence of Groups with a Key and a sequence of SomeId objects
// order the Groups by StartDate, This StartDate is in the Key
.OrderBy(group => group.Key);
By the way, if you don't want a Key
, and insist on having a StartDate
, there is a less known overload of GroupBy . 顺便说一句,如果您不想要
Key
,而是坚持拥有StartDate
,那么GroupBy的重载就鲜为人知 。 A version where you can Select what you want in your output. 您可以在其中选择所需内容的版本。
.GroupBy(view = view.StartDate, // make groups with same StartDate
view => view.SomeId, // select SomeId as elements in the group
(commonStartDate, someIds) => new // from every commonStartDate and the someIds
{ // in this group make a new object
StartDate = commonstartDate, // containing this common StartDate
SomeIds = someIds, // and the SomeId objects in the group
})
.OrderBy(group => group.StartDate); // now you can order by StartDate instead of Key
Update: 更新:
You have simple problem in you code, change you code to this: 您的代码中有简单的问题,请将代码更改为此:
var rooms = roomBinding.GroupBy(g => new { Id = g.R_ID, Name = g.r_name })
.Select(g => new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
});
After new in grouping
you have to new and set your data like my sample. grouping
完成后,您必须像我的样本一样新建并设置数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.