【发布时间】:2016-09-27 15:52:29
【问题描述】:
我正在努力摆脱无法将System.Collections.Generic.List 转换为IEnumerable 的问题,下面是我的代码:
IEnumerable<PY_History_TransactionTAB> FilteredReport;
var ReportData = db.PY_History_TransactionTAB
.Where(x => x.SystemCode == SysCode)
.GroupBy(x => x.EmployeeCode);
FilteredReport = (IEnumerable<PY_History_TransactionTAB>)ReportData.Select(x => new
{
EmployeeCode = x.Key,
H_SalaryDays = x.Sum(y => y.H_SalaryDays ?? 0),
H_NET_Overtime = x.Sum(y => y.H_NET_Overtime),
H_Overtime_Amount = x.Sum(y => y.H_Overtime_Amount),
H_SL_Breakup1 = x.Sum(y => y.H_SL_Breakup1 ?? 0),
H_SL_Breakup2 = x.Sum(y => y.H_SL_Breakup2 ?? 0),
H_SL_Breakup3 = x.Sum(y => y.H_SL_Breakup3 ?? 0),
H_OT_Allowance1 = x.Sum(y => y.H_OT_Allowance1 ?? 0),
H_OT_Allowance2 = x.Sum(y => y.H_OT_Allowance2 ?? 0),
H_OT_Allowance3 = x.Sum(y => y.H_OT_Allowance3 ?? 0),
H_OT_Allowance4 = x.Sum(y => y.H_OT_Allowance4 ?? 0),
H_OT_Allowance5 = x.Sum(y => y.H_OT_Allowance5 ?? 0)
}).ToList();
当我运行该应用程序时,它会在分配给 FilteredReport 变量时抛出运行时异常System.InvalidCastException,方法是:
{"无法转换类型为 'System.Collections.Generic.List
1[<>f__AnonymousType665 的对象 [System.String,System.Decimal,System.Nullable1[System.Decimal],System.Nullable1[System.Decimal], System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal ,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal, System.Decimal,System.Decimal,System.Decimal,System.Decimal,System.Decimal]]' 键入 'System.Collections .Generic.IEnumerable`1[HrAndPayrollSystem.Models.PY_History_TransactionTAB]'。"}
所以,我得到的是我做的不对,我需要找到正确的方法,我应该怎么做才能摆脱这个问题或者将List 转换为@987654328 的正确方法是什么@?任何帮助将不胜感激,在此先感谢!
更新:
好的,René Vogt 对上述问题的回答是正确的,但是我同时遇到了另一个异常 System.NotSupportedException 说:
实体或复杂类型 'HrAndPayrollSystem.Models.PY_History_TransactionTAB' 不能在 LINQ to Entities 查询中构造。
我应该如何解决?
【问题讨论】:
标签: linq-to-sql casting asp.net-mvc-5