2017-03-06 6 views
2

아래 LINQ에는 몇 가지 Convert.ToInt32 방법이 있습니다. 그러나 그것은 효과가 없습니다. 인터넷을 참조하면 변환 대신 int.Parse이 사용됩니다. 여전히 오류가 발생합니다. 이것을 할 방향을 보여주십시오. Convert.ToInt32를 사용하는 경우LINQ를 사용하여 Int로 변환

: LINQ 오류가 발생 위 대한

코드는 엔티티 프레임 워크를 사용하고 있습니다 :

아래 쿼리 tody의 데이터 유형에서

DateTime

var ds = (from a in dbSetInvHeader 
      join b in dbSetCustomer on a.BusinessEntityID equals b.Id 
      join c in dbSetFinancialInfo on b.Id equals c.Id 
      where (a.TotalAmount - a.AppliedAmount - a.ApplyToInvoiceCreditAmount) > 0 
         && DbFunctions.AddDays(a.InvoiceDate, Convert.ToInt32(c.CreditPeriod)) >= tody 
      select new OverDueInvoices 
         { 
          CustomerName = b.Name, 
          InvoiceNo = a.InvoiceNo, 
          InvoiceAmount = a.TotalAmount - a.ApplyToInvoiceCreditAmount, 
          DueAmount = (a.TotalAmount - a.AppliedAmount - a.ApplyToInvoiceCreditAmount), 
          CreditAmount = a.ApplyToInvoiceCreditAmount, 
          NoOfDays = Convert.ToInt32(DbFunctions.DiffDays(tody, a.InvoiceDate)) 
         }).ToList(); 

업데이트입니다

"LINQ to Entities가 'Int32 ToInt32 (System.Decimal)'메서드를 인식하지 못하고이 메서드를 저장소 식으로 변환 할 수 없습니다."

int.Parse 사용 ".있어서 'int32를 파싱 (선택 System.String)'방법 및이 방법을 인식하지 않는 엔티티 LINQ가 저장 식으로 변환 될 수 없다"

+0

null을 변환하는 동안 그래서 당신이 InvalidCastException을 얻을 수 (int)에 캐스팅 피하기 위해 더 좋을 것 Nullable<int> 또는 int?를 반환? –

+0

[EntityFunctions] 시도 (https://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions (v = vs.110) .aspx)? –

+0

"UPDATE 1"에 추가 정보가 더 추가되었습니다. – weeraa

답변

1

SQL은 Convert.ToInt32 함수에 대해 알지 못합니다. Entity Framework에서 문자열을 int로 변환하는 해결 방법이없는 것 같습니다. 당신이 할 수있는 것은 같은

var ds = (from a in dbSetInvHeader 
        join b in dbSetCustomer on a.BusinessEntityID equals b.Id 
        join c in dbSetFinancialInfo on b.Id equals c.Id 
        where (a.TotalAmount - a.AppliedAmount - a.ApplyToInvoiceCreditAmount) > 0 
        && DbFunctions.AddDays(a.InvoiceDate, Convert.ToInt32(c.CreditPeriod)) >= tody 
        select new OverDueInvoices 
        { 
         CustomerName = b.Name, 
         InvoiceNo = a.InvoiceNo, 
         InvoiceAmount = a.TotalAmount - a.ApplyToInvoiceCreditAmount, 
         DueAmount = (a.TotalAmount - a.AppliedAmount - a.ApplyToInvoiceCreditAmount), 
         CreditAmount = a.ApplyToInvoiceCreditAmount, 
         NoOfDaysString = Convert.ToInt32(DbFunctions.DiffDays(tody, a.InvoiceDate)) 

        }).ToList(); 

이 내가 어디에 문에 대해 생각할 수있는 유일한 해결 방법입니다 귀하의 질의를 당신이

public string NoOfDaysString {get; set;} 
public string NoOfDays {get { return Convert.ToInt32(NoOfDaysString); } ;} 

NoOfDaysString

객체에 다른 열을 추가하고 다시 작성합니다. 개체의 CreditPeriod을 추가하고 개체를

var ds = (from a in dbSetInvHeader 
       join b in dbSetCustomer on a.BusinessEntityID equals b.Id 
       join c in dbSetFinancialInfo on b.Id equals c.Id 
       where (a.TotalAmount - a.AppliedAmount - a.ApplyToInvoiceCreditAmount) > 0 
       select new OverDueInvoices 
       { 
        CustomerName = b.Name, 
        InvoiceNo = a.InvoiceNo, 
        InvoiceAmount = a.TotalAmount - a.ApplyToInvoiceCreditAmount, 
        DueAmount = (a.TotalAmount - a.AppliedAmount - a.ApplyToInvoiceCreditAmount), 
        CreditAmount = a.ApplyToInvoiceCreditAmount, 
        NoOfDaysString = Convert.ToInt32(DbFunctions.DiffDays(tody, a.InvoiceDate)) 
        CreditPeriod = c.CreditPeriod 
       })ToList().Where(t=>Convert.ToInt32(t.CreditPeriod) >= NoOfDays).ToList(); 
+0

감사합니다. 실제로 이것은 언급 한대로 작동합니다. 그러나 WHERE 조건에 다른 캐스트 부분이 있습니다. 나는 "intCreditDays"라는 새로운 속성을 포함하여 같은 일을하려고합니다. 그러나 "지정된 유형 멤버 인 'intCreditDays'는 LINQ to Entities에서 지원되지 않으며 이니셜 라이저, 엔터티 멤버 및 엔터티 탐색 속성 만 지원됩니다." 오류가 발생했습니다. – weeraa

+0

당신은 int (int?) c.CreditPeriod @Mikhail Neofitov가 sugested로 캐스팅을 시도 했습니까? –

+0

와우 ... 도움이 ... 감사합니다 ... – weeraa

0

DbFunctions.DiffDays(tody, a.InvoiceDate)Nullable<int>을 반환 메모리에있는 후 곳을, 그래서 대신 Convert.ToInt32의 첫 번째는 다음

DbFunctions.DiffDays(tody, a.InvoiceDate).GetValueOrDefault(); 
DbFunctions.DiffDays(tody, a.InvoiceDate) ?? someIntegerDefaultValue; 
DbFunctions.DiffDays(tody, a.InvoiceDate).Value; 

중 하나를 시도 결과가 null 일 수 있고이 경우 대신 0을 얻으려는 경우에 해당됩니다. 두 번째는 다른 기본값을 원하거나 좀 더 복잡한 대체 값을 계산할 때이며 세 번째는 결과가 null이 아니어야하며 null 인 경우 예외를 얻으려는 경우입니다.

1

@LiviuBoboia가 지적했듯이 SQL은 Convert.ToInt32int.Parse 메쏘드에 대해 아무 것도 모르고 있으며 EF는이 메쏘드의 호출을 SQL로 바꿀 수 없습니다. 이 캐스트는 SQL 함수 CONVERT로 SQL로 변환됩니다이 잘 작동해야

NoOfDaysString = (int)DbFunctions.DiffDays(tody, a.InvoiceDate) 

대신이의 당신은 간단한 캐스트를 할 수 있습니다.

있지만, DbFunctions.DiffDays 오류 발생 될 것입니다 그 무엇 int

+0

'int?'는 Nullable 에 대한 구문 설탕입니다. – grek40

+0

@ grek40 예, 알고 있습니다. 내 생각에 OP는 실제 이름 대신이 구문 설탕 버전을 더 잘 알고 있습니다. –