无法转换类型为'System.Collections.Generic.List`1 [<> f__AnonymousType6`65 [System.String,System.Decimal,System.Nullable`1]的对象

时间:2016-09-27 15:52:29

标签: linq-to-sql casting asp.net-mvc-5

我正试图摆脱这个我无法将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__AnonymousType6的对象65    [System.String,System.Decimal,System.Nullable 1[System.Decimal],System.Nullable 1 [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转换成什么是正确的方法IEnumerable?任何帮助将深表感谢,提前致谢!

更新

好的,RenéVogt的答案对于上述问题是正确的,但后来又遇到了另一个例外System.NotSupportedException

  

实体或复杂类型    'HrAndPayrollSystem.Models.PY_History_TransactionTAB'    不能在LINQ to Entities查询中构造。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

原因是您返回匿名类型List。因此,此List<anonymousType>IEnumerable<HrAndPayrollSystem.Models.PY_History_TransactionTAB>完全不同。

因此,您需要将Select来电更改为:

FilteredReport = (IEnumerable<PY_History_TransactionTAB>)ReportData.Select(x => 
    new PY_History_TransactionTAB // specify type!!
    {
       EmployeeCode = x.Key,
       // shortened for brevity... set properties appropriatly
    }).ToList();

现在返回的列表的类型为List<PY_History_TransactionTAB>,它实现了IEnumerable<PY_History_TransactionTAB>

相关问题