2017-03-06 98 views
2

下面的LINQ有幾個Convert.ToInt32方法。但它不起作用。參考互聯網,它使用int.Parse而不是轉換。仍然給出錯誤。請告訴我一個方向來做到這一點。使用LINQ轉換爲Int

在下面查詢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引發錯誤:

當使用Convert.ToInt32

「LINQ to Entities不識別方法'Int32 ToInt32(System.Decimal)'方法,並且此方法不能轉換爲存儲表達式。」

當使用int.Parse

「LINQ實體無法識別方法‘的Int32解析(System.String)’方法,和這種方法不能被翻譯成表達店」。

+0

什麼是多數民衆贊成拋出的錯誤? –

+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的解決方法。你可以做的是另一列添加到您反對NoOfDaysString

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

和重寫查詢這樣

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

這是唯一的解決方法,我可以在這裏聲明想到的。添加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」的新屬性。但「LINQ to Entities不支持指定的類型成員'intCreditDays',只支持初始化器,實體成員和實體導航屬性。」發生了錯誤。 – weeraa

+0

你試過把它作爲int(int?)c.CreditPeriod作爲@Mikhail Neofitov消化嗎? –

+0

WoW ...這有幫助...謝謝... – 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而不是此案例。第二個是當你想要一個不同的默認值或者計算一些更復雜的替換時,第三個是你知道結果永遠不會爲空的情況,如果它爲空則可以得到異常。

1

由於@LiviuBoboia指出,SQL不知道這兩個Convert.ToInt32int.Parse方法什麼,EF不能正確轉換調用此方法爲SQL。取而代之的是,你可以做簡單的轉換:

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

這個轉換將被轉換爲SQL作爲SQL函數CONVERT,這應該工作良好。

雖然DbFunctions.DiffDays返回Nullable<int>int?所以這將是更好地避免鑄造(int),你可以得到InvalidCastException試圖null轉換爲int

+0

'int?'是'Nullable '的語法糖。 – grek40

+0

@ grek40是的,我知道。我想,OP更熟悉這個語法糖的版本,而不是實際的名字。 –