【发布时间】:2018-09-25 04:26:59
【问题描述】:
我有一个应用程序可以将客户与库存中的车辆进行匹配。有 3 个主表:Customer、Match 和 Inventory。匹配记录包含特定客户和库存记录的估计每月付款。一个客户可以与 Inventory 中的多辆车匹配。
匹配记录包含一个 CustomerId 和一个 InventoryId 以及一个 MonthlyPayment 字段和一些其他杂项字段。
Customer 和 Match 之间存在一对多的关系。 Inventory 和 Match 之间存在一对多的关系。
对于每个客户,我想选择客户记录、每月付款最低的匹配记录以及该匹配的库存记录。
最好的方法是什么?可以通过单个查询完成吗?
我试过这段代码,但实体框架无法评估它,它会在本地执行它,这会影响性能。
var bestMatches = _matchRepository.GetAll(customerMatchSummaryRequest)
.Where(match =>
(_matchRepository.GetAll(customerMatchSummaryRequest)
.GroupBy(m => new { m.Bpid, m.BuyerId, m.CurrentVehicleId })
.Select(g => new
{
g.Key.Bpid,
g.Key.BuyerId,
g.Key.CurrentVehicleId,
LowestMonthlyPayment = g.Min(m => m.MonthlyPayment)
})
.Where(m => m.Bpid == match.Bpid
&& m.BuyerId == match.BuyerId
&& m.CurrentVehicleId == match.CurrentVehicleId
&& m.LowestMonthlyPayment == match.MonthlyPayment)
).Any())
.Include(m => m.Buyer)
.Include(m => m.Inventory);
我在单步调试调试器时收到以下输出:
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'GroupBy(new <>f__AnonymousType2`3(Bpid = [<generated>_2].Bpid, BuyerId = [<generated>_2].BuyerId, CurrentVehicleId = [<generated>_2].CurrentVehicleId), [<generated>_2])' could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'GroupBy(new <>f__AnonymousType2`3(Bpid = [<generated>_2].Bpid, BuyerId = [<generated>_2].BuyerId, CurrentVehicleId = [<generated>_2].CurrentVehicleId), [<generated>_2])' could not be translated and will be evaluated locally.
【问题讨论】: