c# - EF Core 条件(添加)包含到 IQueryable
问题描述
在 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));
}
在 EF Core 中无法覆盖IQueryable
,因为它更“类型安全”
第一行返回 a IIncludableQueryable<Order, User>
,所以当我做第二个 Include 时,它想让它变得不同,例如IIncludableQueryable<Ordr,User,ProductOrder>
我主要有一个GetByIdWithCrudRelations
包含一组布尔值来选择要包含的内容和不包含的内容。有时它只有两个,但在这种情况下它有 8 个,这意味着如果我需要 if-else 一切,它可能会有很多不同的结果。
有人对此有聪明的解决方案吗?
解决方案
您可以使用完全相同的模式。只需从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);
}
请注意,由于ThenInclude
chain 不是嵌套的,因此不需要不同的变量名x
,y
等z
- 单个x
或类似的也可以。
此外,由于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);
推荐阅读
- flutter - Flutter:仅检测图像中的手势
- angular - 如何在 dx Gallery 中绑定数据源?
- node.js - 查找快速应用的热门搜索
- git - git push 错误:src refspec main 与 linux 上的任何内容都不匹配
- buildfire - 从 Widget 端获取 BuildFire 插件用户列表
- spring - Javers 查询返回空结果
- c# - Unity 2D 我可以从游戏中上传图片吗?
- python - Python函数在调用它时不能在另一个函数中工作
- c - 在c编程中无法在空格后写句子
- java - oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:513)