不包含 GroupBy 的定义

Does Not Contain Definition for GroupBy

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<FPSinformation> does not contain a definition for 'GroupBy' and the best extension method overload ParallelEnumerable.GroupBy<string, int>(ParallelQuery<string>, Func<string, int>) requires a receiver of type ParallelQuery<string>

我做错了什么?

编辑

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

这是我要优化的实际代码

中将字符串更改为 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;
    }

此代码有 2 个问题。

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

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

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

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