error 'Int32 Parse(System.String)'
本文关键字:String System Int32 error Parse | 更新日期: 2023-09-27 18:35:37
我是学习LINQ的新手。我在编码方面有一些问题。我使用 Convert.Int32 将列从字符串转换为整数。我的返回函数使用列表。因此,在我的代码中,我确实将查询数据移动到List,但是当我运行此代码时遇到错误时。并且错误始终在"foreach"上,并显示错误消息:
LINQ to 实体无法识别方法"Int32 ToInt32(System.String)"方法,并且此方法无法转换为存储表达式。
这是我的代码:
var query = from o in _invoiceRepository.Table
join opv in _opvRepository.Table on o.Id equals opv.InvoiceId
join pv in _pvRepository.Table on opv.ProductVariantId equals pv.Id
join p in _pRepository.Table on pv.ProductId equals p.Id
where !o.Deleted && EndDate >= o.CreatedOnUtc && StartDate <= o.CreatedOnUtc
&& opv.SubstitutedBy != null && o.PaidDateUtc != null
orderby o.GHOrderId, pv.Sku
select new
{
OrderId = o.GHOrderId,
SKU = pv.Sku,
Brand = p.BrandII,
Description = pv.Description,
Size = p.Size,
OrderQty = Convert.ToInt32(opv.OrderQuantity)
};
var query2 = from row in query
group row by new
{ row.OrderId, row.SKU, row.Brand, row.Description, row.Size } into result
select new
{
OrderId = result.Key.OrderId,
SKU = result.Key.SKU,
Brand = result.Key.Brand,
Description = result.Key.Description,
Size = result.Key.Size,
OrderQty = result.Sum(row => row.OrderQty).ToString("n2")
};
List<string> _tempdata = new List<string>();
foreach (var order in query.ToList())
{
_tempdata.Add(order.OrderId.ToString() + ';' + order.SKU.ToString() + ';' + order.Brand.ToString() + ';' + order.Description.ToString() + ';' + order.Size.ToString() + ';' + order.OrderQty.ToString());
}
我不知道如何解决这个问题...我需要你的帮助。。。
谢谢。
正如异常所述,您不能在 linq to 实体中调用此方法(或大多数其他方法)。这是因为您键入的代码必须转换为 sql,并且除了一些手动移植的方法外,无法将 .net 方法转换为 sql
但是,您可以做的是在 linq to 实体查询中执行所需的所有操作,然后调用 .AsEnumerable()。超过该语句的任何内容都将在 .net 端运行,而不是在 sql 服务器上运行