在Linq中使用group计算列的总和

时间:2014-01-31 17:29:15

标签: c# linq

我有一个包含这种结构的列表:

public class Factor
    {
        public DateTime? date { set; get; }
        public string InnovoiceId { set; get; }
        public string explain { set; get; }
        public string tax { set; get; }
        public string transport { set; get; }
        public Int64? bedehkar { set; get; }
        public Int64? bestankar { set; get; }
        public Int64? mande { set; get; }
    }

        List<Factor> factors = new List<Factor>();

我创建了一个查询,根据 InnovoiceId 对记录进行分组。并计算的总和以及传输的总和bedehkar bestankar mande

我的查询是:

  List<Factor> factorSortList =
           factors.OrderBy(o => o.date)
                  .ToList()
                  .GroupBy(i => i.InnovoiceId)
                  .Select(g => new { tax = g.Sum(i => Convert.ToInt32(i.tax)), transport = g.Sum(i => Convert.ToInt32(i.transport)), bedehkar = g.Sum(i => i.bedehkar), bestankar = g.Sum(i => i.bestankar), mande = g.Sum(i => i.mande) });

但我收到了这个错误:

   Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<samtaApplication.BuLayer.Payment.ReportPayment.Factor>'. An explicit conversion exists (are you missing a cast?)

3 个答案:

答案 0 :(得分:0)

更改这些陈述:

tax = g.Sum(i => i.tax), transport = g.Sum(i => i.transport),

为:

tax = g.Sum(i => Convert.ToInt32(i.tax)), transport = g.Sum(i => Convert.ToInt32(i.transport)),

您在Select语句中创建了一个匿名类型,但是您尝试将其分配给List,更改为:

.Select(g => new Factory { .. });

但是,您的Sum函数将返回int,并且您的属性类型为string。如果您不需要List<Factory>,那么您应该转换它们。使用var:

的隐式类型定义
var  factorSortList = ...

如果您需要List<Factor>

,这是完整的陈述
List<Factor> factorSortList =
       factors.OrderBy(o => o.date)
              .ToList()
              .GroupBy(i => i.InnovoiceId)
              .Select(g => new Factor () 
                      {
                            tax = g.Sum(i => Convert.ToInt32(i.tax)).ToString(),
                            transport = g.Sum(i => Convert.ToInt32(i.transport)).ToString(),
                            bedehkar = g.Sum(i => i.bedehkar),
                            bestankar = g.Sum(i => i.bestankar),
                            mande = g.Sum(i => i.mande)
                      });

答案 1 :(得分:0)

您正在创建匿名类型

.Select(g => new { ... });

并假装它是Factor

List<Factor> factorSortList = ...

您需要使用var,因为它是匿名类型或在该select语句中创建不同的类型。

以下是解决此问题的一种方法,以及您的Sum个变量中的两个string而不是数字(假设为int这一事实,但您可能需要此处是double

var factorSortList =
       factors.OrderBy(o => o.date)
              .ToList()
              .GroupBy(i => i.InnovoiceId)
              .Select(g => new { tax = g.Sum(i => Convert.ToInt32(i.tax)), transport = g.Sum(i => Convert.ToInt32(i.transport)), bedehkar = g.Sum(i => i.bedehkar), bestankar = g.Sum(i => i.bestankar), mande = g.Sum(i => i.mande) });

或者,如果此处的汇总类型是Factor ...

              .Select(g => new Factor() { tax = g.Sum(i => Convert.ToInt32(i.tax)).ToString(), transport = g.Sum(i => Convert.ToInt32(i.transport)).ToString(), bedehkar = g.Sum(i => i.bedehkar), bestankar = g.Sum(i => i.bestankar), mande = g.Sum(i => i.mande) }).ToList();

答案 2 :(得分:0)

您正在使用Select语句创建匿名类型,该语句无法隐式转换为Factor的实例。试试Select新的Factor,如下所示:

List<Factor> factorSortList =
           factors.OrderBy(o => o.date)
                  .ToList()
                  .GroupBy(i => i.InnovoiceId)
                  .Select(g => new Factor () 
                          {
                                tax = g.Sum(i => Convert.ToInt32(i.tax)),
                                transport = g.Sum(i => Convert.ToInt32(i.transport)),
                                bedehkar = g.Sum(i => i.bedehkar),
                                bestankar = g.Sum(i => i.bestankar),
                                mande = g.Sum(i => i.mande)
                          });

虽然Factor.taxFactor.transportstring,但您需要为这些.ToString()的结果添加.Sum