linq2sql 中的简单急切/延迟加载示例
有人在 linq2sql 中有一个简单的代码示例来演示急切加载和延迟加载之间的区别吗?
Does anyone have a simple code example in linq2sql that demonstrates the difference between Eager Loading and Lazy Loading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
延迟加载:对于给定实体,首次加载时其关联集合可能为空,但首次迭代这些集合时,LINQ to SQL 会触发查询以在之后加载这些集合加载后,集合可供将来使用,无需再次查询:
OrderDetails
仅在迭代时才加载,因此如果OrderDetails
是never iterated 相应的查询永远不会执行。预加载:立即加载所有引用实体的关联集合,例如 LINQ to SQL 将自动为所有检索的订单带来所有
OrderDetails
请注意:延迟执行是 LINQ 功能,但延迟加载是 LINQ to SQL 功能
Deferred Loading: for a given entity, it's associated collections may be empty when it is first loaded, but when these collections are first iterated, LINQ to SQL fires off a query to load these collections after it is loaded the collection is then available for future use without requiring another query:
The
OrderDetails
are loaded only if it is iterated, so If theOrderDetatils
are never iterated the corresponding query is never executed.Eager Loading: immediate loading the associated Collections for all referenced entities for example LINQ to SQL will automatically brings all the
OrderDetails
for all the retreived Ordersnote that: Deferred execution is a LINQ Feature but Deferred Loading is a LINQ to SQL feature