[英]ThenInclude for Explicit Loading in Entity Framework core?
I know I can go one step deeper to load related data using ThenInclude
in Eager Loading like below example我知道我可以更深入地使用
ThenInclude
in Eager Loading 加载相关数据,如下例所示
//Eager Loading
var publisher = await _context.Publishers
.Include(pub => pub.Books)
.ThenInclude(book => book.Sales)
.Include(pub => pub.Users)
.Where(pub => pub.PubId == id)
.FirstOrDefaultAsync();
How can I write the same query in Explicit Loading?如何在显式加载中编写相同的查询? How do I load data for
Sales
without looping through books in below case?在以下情况下,如何在不遍历书籍的情况下加载
Sales
数据?
//Explicit Loading
var publisher = await _context.Publishers
.SingleAsync(pub => pub.PubId == id);
_context.Entry(publisher)
.Collection(pub => pub.Books)
.Load();
_context.Entry(publisher)
.Collection(pub => pub.Users)
.Load();
Query() method is your friend. Query()方法是你的朋友。
It's partially explained in the Querying related entities subsection of Explicit loading documentation:它在显式加载文档的查询相关实体小节中进行了部分解释:
You can also get a LINQ query that represents the contents of a navigation property.
您还可以获得表示导航属性内容的 LINQ 查询。
This allows you to do things such as running an aggregate operator over the related entities without loading them into memory.
这允许您执行诸如在相关实体上运行聚合运算符之类的操作,而无需将它们加载到内存中。
Example...
例子...
You can also filter which related entities are loaded into memory.
您还可以过滤哪些相关实体加载到内存中。
Example...
例子...
What they forgot to mention is that you can use it also for Include
/ ThenInclude
related data of the explicitly loading data.他们忘记提到的是,您也可以将它用于显式加载数据的
Include
/ ThenInclude
相关数据。
eg例如
_context.Entry(publisher)
.Collection(pub => pub.Books)
.Query() // <--
.Include(book => book.Sales) // <--
.Load();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.