C# LINQ to实体无法识别查询中的方法
我有以下代码,它告诉我一个错误,LINQ to Entities无法识别方法GetShippingHour:C# LINQ to实体无法识别查询中的方法,c#,linq,entity-framework,linq-to-entities,C#,Linq,Entity Framework,Linq To Entities,我有以下代码,它告诉我一个错误,LINQ to Entities无法识别方法GetShippingHour: private IEnumerable<OrderDTO> GetAllFixedOrders(DateTime date) { var clientSet = _context.Set<Client>(); var orderSubstitutes = _context.Set<OrderSubstitution&g
private IEnumerable<OrderDTO> GetAllFixedOrders(DateTime date)
{
var clientSet = _context.Set<Client>();
var orderSubstitutes = _context.Set<OrderSubstitution>()
.Include(o=>o.Order.Client)
.ForCurrentDay(date)
.Select(os => new { os.OrderId, os.FixedOrderId });
var orderList = _context.Set<FixedOrder>()
.Include(fo => fo.Client)
.ForCurrentDay(date)
.Where(fo => !fo.Client.IsInactive)
.ConsideringOrderSubstitution(orderSubstitutes.Select(os => os.FixedOrderId).ToList())
.Join(clientSet
, o => o.ClientId
, c => c.Id
, (o, c) =>
new OrderDTO
{
OrderId = orderSubstitutes.Where(os => os.FixedOrderId == o.Id).Select(os => os.OrderId).FirstOrDefault() ?? 0,
ClientId = c.Id,
ClientName = c.Name,
HasFixedOrders = true,
FixedOrderId = o.Id,
ShippingHour = GetShippingHour(c,date),
HasSpecialProducts = false
}
).ToList();
return orderList;
}
private string GetShippingHour(Client c, DateTime date)
{
var dayOfWeek = date.DayOfWeek.ToString();
string result;
switch (dayOfWeek)
{
case "Sunday":
result = c.ShippingHourSunday ?? c.ShippingHour;
break;
case "Monday":
result = c.ShippingHourMonday ?? c.ShippingHour;
break;
case "Tuesday":
result = c.ShippingHourTuesday ?? c.ShippingHour;
break;
case "Wednesday":
result = c.ShippingHourWednesday ?? c.ShippingHour;
break;
case "Thursday":
result = c.ShippingHourThursday ?? c.ShippingHour;
break;
case "Friday":
result = c.ShippingHourFriday ?? c.ShippingHour;
break;
case "Saturday":
result = c.ShippingHourSaturday ?? c.ShippingHour;
break;
default:
result = c.ShippingHour;
break;
}
return result;
}
private IEnumerable GetAllFixedOrders(日期时间日期)
{
var clientSet=_context.Set();
var orderreplacements=_context.Set()
.Include(o=>o.Order.Client)
.ForCurrentDay(日期)
.Select(os=>new{os.OrderId,os.FixedOrderId});
var orderList=_context.Set()
.Include(fo=>fo.Client)
.ForCurrentDay(日期)
.Where(fo=>!fo.Client.IsInactive)
.ConsideringOrderSubstitution(orderSubstitution.Select(os=>os.FixedOrderId.ToList())
.Join(客户端集)
,o=>o.ClientId
,c=>c.Id
,(o,c)=>
新订单
{
OrderId=OrderReplacements.Where(os=>os.FixedOrderId==o.Id)。选择(os=>os.OrderId)。FirstOrDefault()??0,
ClientId=c.Id,
ClientName=c.Name,
HasFixedOrders=true,
FixedOrderId=o.Id,
ShippingHour=GetShippingHour(c,日期),
HasSpecialProducts=错误
}
).ToList();
返回订单列表;
}
私有字符串GetShippingHour(客户端c,日期时间日期)
{
var dayOfWeek=date.dayOfWeek.ToString();
字符串结果;
开关(星期五)
{
“星期日”一案:
结果=c.发货小时数星期日??c.发货小时数;
打破
案件“星期一”:
结果=每天c.发货小时??每天c.发货小时;
打破
案件“星期二”:
结果=c.发货小时数天??c.发货小时数;
打破
案件“星期三”:
结果=c.发货小时星期五??c.发货小时;
打破
案件“星期四”:
结果=c.装运小时周四??c.装运小时;
打破
案件“星期五”:
结果=c.发货小时数周五??c.发货小时数;
打破
“星期六”一案:
结果=c.发货小时数星期天??c.发货小时数;
打破
违约:
结果=c.装运小时;
打破
}
返回结果;
}
问题是,根据一周中的哪一天,我需要从Clients表的不同字段中获取ShippingHour的值。问题是LINQtoEntities试图转换我的方法。我不知道如何重写此查询以获得所需的结果。LINQ to Entities无法将自定义方法转换为SQL。您可以将逻辑拆分为两部分。首先应该从数据库中选择所有需要的字段,并将结果具体化到列表中。具有无法在SQL中处理的自定义逻辑的第二部分进程列表。
var orderList=\u context.Set()
var orderList = _context.Set<FixedOrder>()
.Include(fo => fo.Client)
.ForCurrentDay(date)
.Where(fo => !fo.Client.IsInactive)
.ConsideringOrderSubstitution(orderSubstitutes.Select(os => os.FixedOrderId).ToList())
.Join(clientSet
, o => o.ClientId
, c => c.Id
, (o, c) =>
new
{
OrderId = orderSubstitutes.Where(os => os.FixedOrderId == o.Id).Select(os => os.OrderId).FirstOrDefault() ?? 0,
Client = c,
HasFixedOrders = true,
FixedOrderId = o.Id,
HasSpecialProducts = false
}
).ToList()
.Select(t=>new OrderDTO {
OrderId = t.OrderId,
ClientId = t.Client.Id,
ClientName = t.Client.Name,
HasFixedOrders = t.HasFixedOrders,
FixedOrderId = t.FixedOrderId,
ShippingHour = GetShippingHour(t.Client,date),
HasSpecialProducts = t.HasSpecialProducts
}).ToList();
.Include(fo=>fo.Client)
.ForCurrentDay(日期)
.Where(fo=>!fo.Client.IsInactive)
.ConsideringOrderSubstitution(orderSubstitution.Select(os=>os.FixedOrderId.ToList())
.Join(客户端集)
,o=>o.ClientId
,c=>c.Id
,(o,c)=>
新的
{
OrderId=OrderReplacements.Where(os=>os.FixedOrderId==o.Id)。选择(os=>os.OrderId)。FirstOrDefault()??0,
客户=c,
HasFixedOrders=true,
FixedOrderId=o.Id,
HasSpecialProducts=错误
}
)托利斯先生()
.选择(t=>neworderdto{
OrderId=t.OrderId,
ClientId=t.Client.Id,
ClientName=t.Client.Name,
HasFixedOrders=t.HasFixedOrders,
FixedOrderId=t.FixedOrderId,
ShippingHour=GetShippingHour(t.客户,日期),
HasSpecialProducts=t.HasSpecialProducts
}).ToList();
这是一个很好的答案,因为它向我展示了如何使用LINQ执行某些我不知道可能的技巧。我在这里学到了一些新的东西。“首先应该从数据库中选择所有需要的字段”:当您有数百万行要全部加载时,web服务器或计算机的内存会发生什么变化?@Thomas.Benz我没有写到您需要从数据库中加载所有数据。字段!=排。