C# LINQ:方法'没有重载;GroupBy';接受6个参数/i分组<;t、 t>;不包含定义

C# LINQ:方法'没有重载;GroupBy';接受6个参数/i分组<;t、 t>;不包含定义,c#,asp.net,linq,iqueryable,igrouping,C#,Asp.net,Linq,Iqueryable,Igrouping,问题:获取错误 “方法'GroupBy'不包含6个参数的重载” 很多SO文章都是针对特定的用户创建的方法。GroupBy是库的一部分 我试过改变争论的数量。如果我将其更改为2个参数,则错误会指向下一个区域,在该区域中它具有OrderByDescending并给出错误: “iGroup不包含的定义 “开始日期”和无扩展方法“开始日期”接受第一个 找不到类型为“IGrouping”的参数。“ 给出此错误的代码是: var someVariable = DbContextObject.View.Whe

问题:获取错误

“方法'GroupBy'不包含6个参数的重载”

很多SO文章都是针对特定的用户创建的方法。GroupBy是库的一部分

我试过改变争论的数量。如果我将其更改为2个参数,则错误会指向下一个区域,在该区域中它具有OrderByDescending并给出错误:

“iGroup不包含的定义 “开始日期”和无扩展方法“开始日期”接受第一个 找不到类型为“IGrouping”的参数。“

给出此错误的代码是:

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);

要在ListView中使用,您需要创建匿名对象,其中包含要包含在group by中的所有字段的列表,然后使用grouped list的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);

更新:

您的代码中存在简单问题,请将代码更改为:

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()
                       });

分组中新建后
必须像我的示例一样新建和设置数据。

因此,
分组依据
的输入是一系列
视图
。每个
视图
至少有一个
StartDate
SomeId

您的
GroupBy
使用相同的
StartDate
将所有输入的
视图
分组为从
视图
提取的项目组。每个组都有一个
,其中包含此公共
起始日期
,组中的元素是组中
视图
SomeId

GroupBy
的结果已经是
IQueryable
。因此,
AsQueryable
是不必要的,它只会减慢您的进程

您的
Orderby
的输入是一个组序列。唉,组没有
StartDate
。幸运的是,组中有一个
,其中包含您想要订购的
起始日期

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);
顺便说一下,如果您不想要
,并且坚持要有
起始日期
,那么就有一个。您可以在输出中选择所需内容的版本

.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

听起来你的括号放错地方了。在您的示例中,我没有看到任何带有六个参数的调用。原始的有6个,更新的有2个,这改变了错误。原来是:GroupBy(a=>a.G\u Description,b=>b.G\u ID,c=>c.Plan,d=>d.Start\u Date,e=>e.End\u Date,f=>f.Some\u ID)Hi。我尝试了这个方法,但是在OrderByDescending中,我在开始日期出现了一个错误。错误是:“I分组不包含开始日期的定义谢谢。尝试此I get错误”无法将类型“System.Linq.IQueryable”隐式转换为“System.Linq.IQueryable”///method签名是公共的IQueryable@seesharp删除可查询的方法,因为您查询的是可查询的结果。那么返回的类型是什么?@seesharp您知道EF何时返回数据吗??当您使用FirstOrDefault()、First()、SingleOrDefault()、Single()、ToList()、ToListSync()或类似的方法时。现在,如果不使用这些方法,则结果是可查询的。现在您的返回类型是IQueryable。