【问题标题】:Unable to cast object of type 'System.Collections.Generic.List`1 [<>f__AnonymousType6`65[System.String,System.Decimal,System.Nullable`1无法转换“System.Collections.Generic.List`1 [<>f__AnonymousType6`65[System.String,System.Decimal,System.Nullable`1”类型的对象
【发布时间】: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.List1[&lt;&gt;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


    【解决方案1】:

    原因是您返回了一个匿名类型List。所以这个List&lt;anonymousType&gt;IEnumerable&lt;HrAndPayrollSystem.Models.PY_History_TransactionTAB&gt; 是完全不同的类型。

    因此,您需要将您的 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&lt;PY_History_TransactionTAB&gt; 类型,它实现了IEnumerable&lt;PY_History_TransactionTAB&gt;

    【讨论】:

    • 感谢您的回复。所以,这听起来很对,现在我遇到另一个异常,请参考我更新的问题。
    • @BilalAhmed,如果这个答案解决了你的问题,它应该被接受。如果您还有其他问题,请再问一个问题
    • @StephenMuecke 我的错!
    • @StephenMuecke ,你能导航到这个question
    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2017-11-24
    • 2023-04-03
    相关资源
    最近更新 更多