【问题标题】:'System.Linq.IQueryable' does not contain a definition for 'GroupBy'“System.Linq.IQueryable”不包含“GroupBy”的定义
【发布时间】:2013-10-17 10:56:41
【问题描述】:

我遇到了一个奇怪的问题,我正在编写一个小方法来使用 System.Linq.Dynamic 类来查询对象列表,尽管我认为我的问题不在于动态命名空间。由于某种原因,无法找到 GroupBy 和 OrderByDescending。我已经引用了 System.Linq,这是我最初认为会导致此问题的原因……奇怪的是,它正在寻找 OrderBy 和其他 Linq 方法……我的代码如下。

using System;
using System.Collections.Generic;
using System.Linq.Dynamic;
using System.Linq;
using System.Web;

namespace MvcTemplate.jrSite.Core
{
    public static class DynamicQuery
    {
        public static IQueryable Query(IQueryable items, string where, string select, string groupby, string orderby, string orderbydesc, int take)
        {
            var ret = items;

            if (where != null && where.Length != 0)
            {
                ret = ret.Where(where);
            }

            if (select != null && select.Length != 0)
            {
                ret = ret.Select(select);
            }

            if (groupby != null && groupby.Length != 0)
            {
                ret = ret.GroupBy(groupby);
            }

            if (orderby != null && orderby.Length != 0)
            {
                ret = ret.OrderBy(orderby);
            }

            if (orderbydesc != null && orderbydesc.Length != 0)
            {
                ret = ret.OrderByDescending(orderbydesc);
            }

            return ret;
        }
    }
}

【问题讨论】:

    标签: c# linq iqueryable


    【解决方案1】:

    我假设您正在使用System.Linq.Dynamic 项目。找不到您的方法,因为它们不存在。

    1) GroupBy 表单 System.Linq.Dynamic:

    public static IQueryable GroupBy(this IQueryable source, 
        string keySelector, string elementSelector, params object[] values) {
    

    此方法需要三个参数,而您的代码提供了两个 - 您必须提供 elementSelector

    2) 方法OrderByDescending 甚至没有在该库的源代码中声明。并且System.Linq 也不会帮助您,因为您使用的是非通用IQueryable。所有扩展方法仅提供给强类型查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多