2016-09-27 109 views
0

]類型的對象我試圖敲我的腦袋出去這個問題在我不能夠System.Collections.Generic.List轉換爲IEnumerable的,下面是我的代碼:無法投射'System.Collections.Generic.List`1 [<> f__AnonymousType6`65 [System.String,System.Decimal,System.Nullable`1

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(); 

當我運行應用程序,它在分配點拋出一個運行時異常System.InvalidCastExceptionFilteredReport變量,通過說:

{「無法類型'System.Collections.Generic.List [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?任何幫助將深表感謝,感謝提前!

更新:

好吧,勒內·沃格特的答案是對上述問題是正確的,但後來我遇到了另一個異常System.NotSupportedException在同一點的說法:

實體或複雜類型 'HrAndPayrollSystem.Models.PY_History_TransactionTAB' 不能在LINQ to Entities查詢中構建。

我該如何解決?

回答

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>的。

+0

感謝您的回覆。所以,這聽起來非常正確,現在我遇到了另一個異常,請參閱我更新的問題。 –

+0

@BilalAhmed,如果這個答案解決了你的問題,它應該被接受。如果你有另一個問題,然後問另一個問題 –

+0

@StephenMuecke我的壞! –

相關問題