2017-03-06 110 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更熟悉这个语法糖的版本,而不是实际的名字。 –