EF:包含where子句,+ SubIncludes
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF:包含where子句,+ SubIncludes相关的知识,希望对你有一定的参考价值。
这是这里问题的后续问题:Include with where clause。那个问题想要找到所有乘坐公共汽车的清醒乘客
如果没有乘客的WHERE条款,那很简单,就像这样:
var result = Context.Busses.Where(x => x.IsDriving)
.Include(x => x.Passengers);
如果没有乘客的WHERE条款,包含乘客的子关系也很简单,如下:
var result = Context.Busses.Where(x => x.IsDriving)
.Include(x => x.Passengers.CarryOns)
.Include(x => x.Passengers.Luggage);
但是这个问题需要在导航属性上使用WHERE子句。对先前问题的回答是完美的,没有子关系:
var result = Context.Busses.Where(x => x.IsDriving)
.Select(bus => new {bus, Passengers = bus.Passengers.Where(x => x.Awake)})
.AsEnumerable().Select(x => x.bus).ToList();
但是你如何使用这种方法包括乘客的携带和行李?乘客不可查询,所以你不能在这一点上Include
。我试过这样的事情,但第一部分刚被第二部分覆盖:
var bussesQuery = Context.Busses.Where(x => x.IsDriving)
.Include(x => x.Passengers.CarryOns)
.Include(x => x.Passengers.Luggage);
// var check = bussesQuery.ToList();
// The Sub-Relationship data is included here, but the Passengers are not filtered.
var result = bussesQuery
.Select(bus => new {bus, Passengers = bus.Passengers.Where(x => x.Awake)})
.AsEnumerable().Select(x => x.bus).ToList();
// The Sub-Relationship data is missing, but the Passengers are filtered
通过查看您之前的查询,我看到您从N + 1的极端到单个查询的极端。您曾经有过很多疑问,现在您想拥有一个,但请考虑一下发生了什么。要获取数据实体框架需要交叉连接所有实体,因此对于每个包含的实体,您在结果中获得额外的列,并且结果与该包含交叉连接。
假设您有5辆公交车,30名清醒乘客和每辆公共汽车15辆行李,因此您可以获得公共汽车x行李箱x乘客= 2250条记录,每条记录包含乘客和行李数据。如果您使用单独的查询查询乘客和行李,您将获得更少的记录(5 * 30 + 5 * 15 = 225),并且每个实体将被提取一次。
做一个将返回所有内容的大查询并不是一个好主意 - 它更慢,更难维护而且不值得花时间。只需查询清醒的乘客,然后查询行李。
要获得答案,请向下滚动到答案部分。
免责声明:我喜欢EF。对于我系统中99.999%的调用,我可以最快地编写代码(LINQ),OR-Mapping是最快的系统。此外,生成的查询(虽然令人困惑地看)比手写SQL具有更快的执行计划。但这不是这种情况。
Research Section
从Aside开始:查看我的最终请求的原始SQL是这样的:
SELECT * FROM [Busses] [bus]
LEFT JOIN [Passengers] [passenger] ON [passenger].[BusID] = [bus].[BusID] AND [passenger].[Awake] <> 1
LEFT JOIN [CarryOns] [carryOn] ON [carryOn].[PassengerID] = [passenger].[PassengerID]
LEFT JOIN [Luggages] [luggage] ON [luggage].[PassengerID] = [passenger].[PassengerID]
WHERE [bus].[IsDriving] = 1
当然,如果EF要为这些结果生成某些内容,则需要嵌套和关键字段来了解如何映射它们。没什么大不了。
不幸的是,为了通过单击数据库实现这一点,我必须执行以下操作:
var busses = context.Set<BusEntity>().Where(x => x.IsDriving);
var passengers = context.Set<PassengerEntity>().Where(x => x.Awake);
var carryOns = context.Set<CarryOnEntity>();
var luggages = context.Set<LuggageEntity>();
var passengerJoins = passengers.GroupJoin(
carryOns,
x => x.PassengerID,
y => y.PassengerID,
(x, y) => new { Passenger = x, CarryOns = y }
)
.SelectMany(
x => x.CarryOns.DefaultIfEmpty(),
(x, y) => new { Passenger = x.Passenger, CarryOns = x.CarryOns }
).GroupJoin(
luggages,
x => x.Passenger.PassengerID,
y => y.PassengerID,
(x, y) => new { Passenger = x.Passenger, CarryOns = x.CarryOns, Luggages = y }
)
.SelectMany(
x => x.Luggages.DefaultIfEmpty(),
(x, y) => new { Passenger = x.Passenger, CarryOns = x.CarryOns, Luggages = x.Luggages }
);
var bussesToPassengers = busses.GroupJoin(
passengerJoins,
x => x.BusID,
y => y.Passenger.BusID,
(x, y) => new { Bus = x, Passengers = y }
)
.SelectMany(
x => x.Passengers.DefaultIfEmpty(),
(x, y) => new { Bus = x.Bus, Passengers = x.Passengers }
)
.GroupBy(x => x.Bus);
var rez = bussesToPassengers.ToList()
.Select(x => x.First().Bus)
.ToList();
我不抱怨EF Generated SQL,但单个SQL语句是几百行。我攻击它,删除SELECT列,并更改了一些ID以匹配此问题,它是这样的:
SELECT *
FROM ( SELECT *
FROM (SELECT *
FROM ( SELECT DISTINCT *
FROM [dbo].[Bus] AS [Extent1]
LEFT OUTER JOIN (SELECT *
FROM [dbo].[Passenger] AS [Extent2]
LEFT OUTER JOIN [dbo].[CarryOns] AS [Extent3] ON [Extent2].[PassengerId] = [Extent3].[PassengerId]
LEFT OUTER JOIN [dbo].[Luggages] AS [Extent4] ON [Extent2].[PassengerId] = [Extent4].[PassengerId]
WHERE [Extent1].[IsDriving] = 1
) AS [Distinct1] ) AS [Project2]
OUTER APPLY (SELECT *
FROM (SELECT *
FROM [dbo].[Bus] AS [Extent6]
LEFT OUTER JOIN (SELECT *
FROM [dbo].[Passenger] AS [Extent7]
LEFT OUTER JOIN [dbo].[CarryOns] AS [Extent8] ON [Extent7].[PassengerId] = [Extent8].[PassengerId]
LEFT OUTER JOIN [dbo].[Luggages] AS [Extent9] ON [Extent7].[PassengerId] = [Extent9].[PassengerId]
WHERE ([Extent6].[IsDriving] = 1) AND ([Project2].[BusId] = [Extent6].[BusId]) ) AS [Project3]
OUTER APPLY (SELECT *
FROM [dbo].[Passenger] AS [Extent11]
LEFT OUTER JOIN [dbo].[CarryOns] AS [Extent12] ON [Extent11].[PassengerId] = [Extent12].[PassengerId]
LEFT OUTER JOIN [dbo].[Luggages] AS [Extent13] ON [Extent11].[PassengerId] = [Extent13].[PassengerId]
LEFT OUTER JOIN [dbo].[CarryOns] AS [Extent15] ON [Extent11].[PassengerId] = [Extent15].[PassengerId]
WHERE ([Extent11].[IsAwake] = 1) AND ([Project3].[BusId] = [Extent11].[BusId])
UNION ALL
SELECT *
FROM [dbo].[Passenger] AS [Extent16]
LEFT OUTER JOIN [dbo].[CarryOns] AS [Extent17] ON [Extent16].[PassengerId] = [Extent17].[PassengerId]
LEFT OUTER JOIN [dbo].[Luggages] AS [Extent18] ON [Extent16].[PassengerId] = [Extent18].[PassengerId]
WHERE ([Extent16].[IsAwake] = 1) AND ([Project3].[BusId] = [Extent16].[BusId])
) AS [Project7]
ORDER BY ........................
对于我的个人测试数据,My Hand-Written SQL Query返回54行,EF Generated Query返回大约30,000行。因此,如果您只考虑数据的线上传输时间的增加,那是不可接受的。
Answer Section
答案是:您可以使用Linq to Entities(在DB上)和Linq to Objects(在代码中)在单个调用中实现您的结果,但它不会具有高效性。您可以选择具有更好性能的多个调用,包括通过线路传输的数据更少,生成的查询更易读,以及更易理解的代码。
最好的办法是执行多个查询。这就是我这样做的方式:
var bus = context.Set<BusEntity>().Where(x => x.IsDriving).ToList();
var busIDs = bus.Select(x => x.BusID).ToList();
var passengers = context.Set<PassengerEntity>().Where(x => x.IsAwake && busIDs.Contains(x.BusID)).ToList();
var passengerIDs = passengers.Select(x => x.PassengerID).ToList();
var carryOns = context.Set<CarryOnEntity>().Where(x => passengerIDs.Contains(x.PassengerID)).ToList();
var luggages = context.Set<LuggageEntity>().Where(x => passengerIDs.Contains(x.PassengerID)).ToList();
passengers.ForEach(x => {
x.CarryOns = carryOns.Where(y => y.PassengerID == x.PassengerID).ToList();
x.Luggages = luggages.Where(y => y.PassengerID == x.PassengerID).ToList();
});
bus.ForEach(x => x.Passengers = passengers.Where(y => y.BusID == x.BusID).ToList());
这产生了4个电话。总而言之,SQL大约有40行。我攻击它,删除SELECT列,并更改了一些ID以匹配此问题,它是这样的:
SELECT * FROM [dbo].[Busses] AS [Extent1]
WHERE [Extent1].[IsDriving] = 1
SELECT * FROM [dbo].[Passengers] AS [Extent1]
WHERE ([Extent1].[Awake] = 1) AND ([Extent1].[BusID] IN (......................))
SELECT * FROM [dbo].[CarryOns] AS [Extent1]
WHERE [Extent1].[PassengerID] IN (......................)
SELECT * FROM [dbo].[Luggages] AS [Extent1]
WHERE [Extent1].[PassengerID] IN (......................)
EF生成的查询在4次往返调用中总共返回100行。这意味着4次调用数据库,但都非常小,可读且非常快。
我没有时间,但每当我在这个答案的代码上方的断点处暂停,而F5到达结果的另一侧时,它就是即时的。当我在我的研究中为单一呼叫做同样的事情时,它花费了第二次或更多,显着滞后。
以上是关于EF:包含where子句,+ SubIncludes的主要内容,如果未能解决你的问题,请参考以下文章