【问题标题】:LINQ: No overload for method 'GroupBy' takes 6 arguments / IGrouping<t,t> does not contain a definitionLINQ:方法“GroupBy”没有重载需要 6 个参数/IGrouping<t,t> 不包含定义
【发布时间】:2018-10-03 21:54:41
【问题描述】:

问题:出现错误

“方法 'GroupBy' 没有重载需要 6 个参数”

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

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

"IGrouping 不包含对 'Start_Date' 并且没有扩展方法 'Start_Date' 接受第一个 可以找到“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 中使用

【问题讨论】:

  • 听起来好像你在错误的位置放了一个括号。在您的示例中,我没有看到任何带有六个参数的调用。
  • 原来有6个,更新有2个,改错了。原来是:GroupBy(a=>a.G_Description, b=>b.G_ID, c=>c.Plan, d=> d.Start_Date, e=> e.End_Date, f=>f.Some_ID)跨度>

标签: c# asp.net linq iqueryable igrouping


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    所以GroupBy 的输入是Views 的序列。每个View 至少有一个StartDateSomeId

    您的GroupBy 将所有输入Views 分组为从Views 中提取的具有相同StartDate 的项目组。每个 Group 都有一个 Key 包含这个常见的StartDate,组中的元素是组中ViewsSomeId

    GroupBy 的结果已经是IQueryable&lt;...&gt;。所以AsQueryable 是不必要的,它只会减慢你的进程。

    Orderby 的输入是一组组。唉,群组没有StartDate。幸运的是,群组有一个 Key,其中包含您想要订购的 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);
    

    顺便说一句,如果您不想要Key,而坚持拥有StartDate,则有less known overload of GroupBy。一个版本,您可以在其中选择您想要的输出。

    .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
    

    【讨论】:

      【解决方案3】:

      更新:

      你的代码有简单的问题,把你的代码改成这样:

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

      grouping 中新建后,您必须像我的示例一样新建并设置数据。

      【讨论】:

      • 嗨。我试过这个,但在 OrderByDescending 时,我在 Start_Date 上收到错误。错误是:“IGrouping, View> 不包含 Start_Date 的定义
      • 谢谢。尝试这个我得到错误“不能隐式转换类型 'System.Linq.IQueryable>' to 'System.Linq.IQueryable' //// / 方法签名是 public IQueryable
      • @seesharp Remove AsQueryable 方法,因为你查询的是可查询的结果。
      • 那么返回类型是什么?
      • @seesharp 你知道EF什么时候返回数据吗?当您使用 FirstOrDefault()、First()、SingleOrDefault()、Single()、ToList()、ToListAsync() 或类似这些方法时。现在,如果你不使用这些方法,你的结果就是 IQueryable。现在你的返回类型是 IQueryable.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2018-12-03
      • 2012-02-23
      相关资源
      最近更新 更多