2016-09-27 1 views
0

나는 나가기 위해 내 머리를 치려고합니다. 나는 응용 프로그램을 실행할 때'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(); 

, 그것은 FilteredReport 변수에 할당 시점에서 런타임 예외 System.InvalidCastException를 던졌습니다 : 나는 IEnumerableSystem.Collections.Generic.List을 변환 할 수 아니에요있는이 문제의 아래에 내 코드입니다 말함으로써 :

{ "Unable '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 을 입력하려면 System.Decimal, System.Decimal, System.Decimal, System.Decimal, System.Decimal]] '을 입력하십시오. Generic.IEnumerable`1 [HrAndPayrollSystem.Models.PY_History_TransactionTAB]. "}

그래서, 내가 무엇을 얻을 것은 내가 바로 않을 것이다, 나는 얻기 위해 무엇을해야, 올바른 방법을 찾을 필요 이 문제를 없애거나 ListIEnumerable으로 변환하는 올바른 방법은 무엇입니까? 어떤 도움을 주셔서 감사합니다, 미리 감사드립니다!

는 업데이트 :

좋아, 르네 보그의 대답은 위의 문제에 대한 정확하지만 나는이 같은 점 속담에서 또 다른 예외 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(); 

지금 반환 된 목록은 IEnumerable<PY_History_TransactionTAB>을 구현 형 List<PY_History_TransactionTAB>이다.

+0

답변 해 주셔서 감사합니다. 그래서, 이것은 매우 바르게 들립니다. 이제 또 다른 예외가 있습니다. 제 업데이트 된 질문을 참조하십시오. –

+0

@BilalAhmed,이 답변으로 문제가 해결되면 받아 들여야합니다. 다른 문제가 있다면 다른 질문을하십시오 –

+0

@StephenMuecke 내 잘못입니다! –

관련 문제