繁体   English   中英

LINQ:方法&#39;GroupBy&#39;的重载没有6个参数/ IGrouping <t,t> 不包含定义

[英]LINQ: No overload for method 'GroupBy' takes 6 arguments / IGrouping<t,t> does not contain a definition

问题:出现错误

“方法'GroupBy'的重载不接受6个参数”

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

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

“ IGrouping不包含'Start_Date'的定义,找不到可以接受类型为'IGrouping'的第一个参数的扩展方法'Start_Date'。”

给出此错误的代码是:

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中的所有字段的列表,然后使用分组列表的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);

因此, GroupBy的输入是一系列Views 每个View至少都有一个StartDateSomeId

您的GroupBy将所有输入的Views分组为从具有相同StartDateViews提取的项目组。 每个组都有一个包含此公共StartDateKey ,该组中的元素是该组中ViewsSomeId

GroupBy的结果已经是IQueryable<...> 因此, AsQueryable是不必要的,只会减慢您的处理速度。

您的Orderby的输入是一组序列。 groups,群组没有StartDate 幸运的是,组具有包含您要作为排序依据的StartDateKey

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 ,那么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

更新:

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

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完成后,您必须像我的样本一样新建并设置数据。

暂无
暂无

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

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