使用LINQ转换为Int
下面的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)’方法,和这种方法不能被翻译成表达店”。
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();
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
而不是此案例。第二个是当你想要一个不同的默认值或者计算一些更复杂的替换时,第三个是你知道结果永远不会为空的情况,如果它为空则可以得到异常。
由于@LiviuBoboia指出,SQL不知道这两个Convert.ToInt32
和int.Parse
方法什么,EF不能正确转换调用此方法为SQL。取而代之的是,你可以做简单的转换:
NoOfDaysString = (int)DbFunctions.DiffDays(tody, a.InvoiceDate)
这个转换将被转换为SQL作为SQL函数CONVERT
,这应该工作良好。
虽然DbFunctions.DiffDays
返回Nullable<int>
或int?
所以这将是更好地避免铸造(int)
,你可以得到InvalidCastException
试图null
转换为int
'int?'是'Nullable
@ grek40是的,我知道。我想,OP更熟悉这个语法糖的版本,而不是实际的名字。 –
什么是多数民众赞成抛出的错误? –
尝试[EntityFunctions](https://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions(v = vs.110).aspx)? –
我在“UPDATE 1”中添加了更多详细信息 – weeraa