我有一个包含这种结构的列表:
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?)
答案 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.tax
和Factor.transport
是string
,但您需要为这些.ToString()
的结果添加.Sum
。