2015-12-16 93 views
0
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(); 
    } 

我试图做这样的事情,但得到的错误不包含定义为的GroupBy

IQueryable<FPSinformation>不包含“的GroupBy” 的定义和最佳推广方法重载 ParallelEnumerable.GroupBy<string, int>(ParallelQuery<string>, Func<string, int>)需要一个类型的接收器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(); 

这是我想实际的代码优化

+0

@Neel它已经存在 –

回答

2

更改字符串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; 
    } 
+0

试过太在这种情况下 '。凡(X => x.Key!= NULL)' 越来越误差不能将int转换成字符串 –

+0

键不能为空每次 –

+0

你可以得到将int更改为int? –

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不能为空。

+0

我的错误,我想按字符串分组,但我想要总结它的数量 –

+0

'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) })' 这是我的,我试图优化实际的代码 –