[英]EF Core conditional (add) includes to an IQueryable
In EF6 I was used to doing this: 在EF6中,我习惯于这样做:
var orders = GetAllEntities().Include(x => x.Contact.User);
if (includeProducts)
{
orders = orders.Include(x => x.ProductOrders.Select(y => y.RentStockOrders));
orders = orders.Include(x => x.ProductOrders.Select(y => y.Product));
orders = orders.Include(x => x.ProductOrders.Select(y => y.Currency));
orders = orders.Include(x => x.ProductOrders.Select(y => y.Coupons));
orders = orders.Include(x => x.AdditionalCosts);
orders = orders.Include(x => x.Partner);
orders = orders.Include(x => x.OrderCoupons.Select(y => y.Coupon.Partner));
if (includeStock)
{
orders = orders.Include(x => x.ProductOrders.Select(y => y.RentStockOrders.Select(z => z.Stock)));
}
}
if (includeInvoices)
{
orders = orders.Include(x => x.Invoices.Select(y => y.Attachments));
}
In EF Core it is not possible to override IQueryable
because it is more 'typesafe' 在EF Core中,无法覆盖IQueryable
因为它更“类型安全”
The first line returns a IIncludableQueryable<Order, User>
, so when I would do the second Include, it wants to make it something different, for example IIncludableQueryable<Ordr,User,ProductOrder>
第一行返回一个IIncludableQueryable<Order, User>
,因此当我执行第二个Include时,它希望使其有所不同,例如IIncludableQueryable<Ordr,User,ProductOrder>
I mostly have a GetByIdWithCrudRelations
which contains a set of bools to choose what to include and what not. 我大多数人都有一个GetByIdWithCrudRelations
,其中包含一组GetByIdWithCrudRelations
以选择要包含的内容和不包含的内容。 sometimes it has only two, but in this case it has 8, which means it can have a lot of different outcomes if I would need to if-else everything. 有时它只有两个,但是在这种情况下,它只有8个,这意味着如果我需要if-else其他所有功能,它可能会有很多不同的结果。
Anyone got a clever solution for this? 任何人都有一个聪明的解决方案吗?
You can use exactly the same pattern. 您可以使用完全相同的模式。 Just start with IQueryable<T>
variable (note that IIncludableQueryable<T, P>
is still IQueryable<T>
with additional ThenInclude
support) and use ThenInclude
instead of nested Select
s: 只需从IQueryable<T>
变量开始(请注意, IIncludableQueryable<T, P>
仍然是IQueryable<T>
并具有其他ThenInclude
支持),然后使用ThenInclude
而不是嵌套的Select
s:
IQueryable<Order> orders = GetAllEntities().Include(x => x.Contact.User);
// or var orders = GetAllEntities().Include(x => x.Contact.User).AsQueryable();
if (includeProducts)
{
orders = orders.Include(x => x.ProductOrders).ThenInclude(y => y.RentStockOrders);
orders = orders.Include(x => x.ProductOrders).ThenInclude(y => y.Product);
orders = orders.Include(x => x.ProductOrders).ThenInclude(y => y.Currency);
orders = orders.Include(x => x.ProductOrders).ThenInclude(y => y.Coupons);
orders = orders.Include(x => x.AdditionalCosts);
orders = orders.Include(x => x.Partner);
orders = orders.Include(x => x.OrderCoupons).ThenInclude(y => y.Coupon.Partner);
if (includeStock)
{
orders = orders.Include(x => x.ProductOrders).ThenInclude(y => y.RentStockOrders).ThenInclude(z => z.Stock);
}
}
if (includeInvoices)
{
orders = orders.Include(x => x.Invoices).ThenInclude(y => y.Attachments);
}
Note that since ThenInclude
chain is not nested, there is no need of different variable names x
, y
, z
etc. - single x
or similar would do the same. 请注意,由于ThenInclude
链没有嵌套,因此不需要使用不同的变量名称x
, y
, z
等ThenInclude
单个x
或类似名称将执行相同操作。
Also since Include
is restarting the include chain from the root, the non conditional assignments like orders = orders.Include(...)
can be combined, eg 同样,由于Include
从根开始重新启动include链,因此可以组合无条件赋值,例如orders = orders.Include(...)
orders = orders
.Include(x => x.ProductOrders).ThenInclude(y => y.RentStockOrders)
.Include(x => x.ProductOrders).ThenInclude(y => y.Product)
.Include(x => x.ProductOrders).ThenInclude(y => y.Currency)
.Include(x => x.ProductOrders).ThenInclude(y => y.Coupons)
.Include(x => x.AdditionalCosts)
.Include(x => x.Partner)
.Include(x => x.OrderCoupons).ThenInclude(y => y.Coupon.Partner);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.