【问题标题】:Does Not Contain Definition for GroupBy不包含 GroupBy 的定义
【发布时间】:2015-12-17 19:25:19
【问题描述】:
public struct CardGrouping
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }

    public List<CardGrouping> GetCardGrouping(IQueryable<Areas.RetailShop.Models.FPSinformation> queryable, Expression<Func<Areas.RetailShop.Models.FPSinformation, string>> groupingFunction)
    {
        return queryable.GroupBy(groupingFunction)
                .Where(x => x.Key != null)
                .Select(x => new CardGrouping
                {
                    Name = x.Key,
                    Count = x.Sum(groupingFunction)
                }).ToList();
    }

我正在尝试做这样的事情,但得到一个错误

IQueryable&lt;FPSinformation&gt; 不包含“GroupBy”的定义 和最好的扩展方法重载 ParallelEnumerable.GroupBy<string, int>(ParallelQuery<string>, Func<string, int>) 需要 ParallelQuery&lt;string&gt; 类型的接收器

我做错了什么?

编辑

var data1 = fpslist.GroupBy(x => x.Ration_Card_Type1)
                .Select(x => new
                {
                    CardType_Name = x.Key,
                    CardType_Count = x.Sum(y => y.Ration_Card_Count1)
                }).ToList();

这是我正在尝试优化的实际代码

【问题讨论】:

  • @Neel 它已经存在了

标签: c# visual-studio func


【解决方案1】:

在 fun 中将字符串更改为 Areas.RetailShop.Models.FPSinformation

public List<CardGrouping> GetCardGrouping(List<Areas.RetailShop.Models.FPSinformation> queryable,
        Expression<Func<Areas.RetailShop.Models.FPSinformation, string>> groupingFunction,
        Func<Areas.RetailShop.Models.FPSinformation, int> sumFunction)

    {


        if (queryable.AsQueryable() != null)
        {

            var data = queryable.AsQueryable().GroupBy(groupingFunction).Where(x => x.Key != null).Select(x => new CardGrouping
            {
                Name = x.Key == null ? "" : x.Key.ToString(),
                Count = x.Sum(sumFunction)
            }).ToList();
            return data;
        }
        return null;
    }

【讨论】:

  • 在这种情况下也尝试过 .Where(x =&gt; x.Key != null) 出现错误,因为无法将 int 转换为字符串
  • key不能每次都为null
  • 你可以把 int 改成 int 吗?
  • 更改了支票,但那我怎么才能得到总和呢?
  • OK 改变int? int 并将您的条件 .Where(x => x.Key != null) 更改为 .Where(x => x.Key !=0)
【解决方案2】:

这段代码有两个问题。

首先,要使其编译,groupingFunction 应该是 Func&lt;FPSinformation, int&gt; - 输入的类型不是 string,而是 FPSinformation

此更改将使其编译,但编译器将选择Enumerable.GroupBy 扩展方法。 Queryable.GroupBy 需要 Expression&lt;Func&gt; 参数,而不是 Func - 所以它应该是 Expression&lt;Func&lt;FPSinformation, int&gt;&gt;

public List<CardGrouping> GetCardGrouping(IQueryable<FPSinformation> queryable, 
                             Expression<Func<FPSinformation, int>> groupingFunction)

您按int 对其进行分组,因此.Where(x =&gt; x.Key != null) 没有意义 - x.Key 不能为空。

【讨论】:

  • 我的错误,我想按我改变的字符串分组,但想总结它的计数
  • var data1 = fpslist.GroupBy(x =&gt; x.Ration_Card_Type1) .Select(x =&gt; new { CardType_Name = x.Key, CardType_Count = x.Sum(y =&gt; y.Ration_Card_Count1) }) 这是我试图优化的实际代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多