wee*_*raa 5 c# linq int entity-framework type-conversion
下面LINQ有几个Convert.ToInt32方法。但这不起作用。参考互联网,使用的是int.Parse而不是转换。仍然给出错误。请告诉我执行此操作的方向。
在下面的查询中,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();
Run Code Online (Sandbox Code Playgroud)
更新 :
该代码使用实体框架来处理上述 LINQ 抛出错误:
使用时Convert.ToInt32:
“LINQ to Entities 无法识别‘Int32 ToInt32(System.Decimal)’方法,并且该方法无法转换为存储表达式。”
使用时int.Parse:
“LINQ to Entities 无法识别‘Int32 Parse(System.String)’方法,并且该方法无法转换为存储表达式。”
SQL 不知道 Convert.ToInt32 函数。实体框架中似乎没有将字符串转换为 int 的解决方法。您可以做的就是向对象 NoOfDaysString 添加另一列
public string NoOfDaysString {get; set;}
public string NoOfDays {get { return Convert.ToInt32(NoOfDaysString); } ;}
Run Code Online (Sandbox Code Playgroud)
并重写您的查询
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();
Run Code Online (Sandbox Code Playgroud)
这是我能想到的 where 语句的唯一解决方法。在对象中添加 CreditPeriod 并在对象进入内存后执行 where
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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14237 次 |
| 最近记录: |