linq to sql loadwith vs associatewith [英] linq to sql loadwith vs associatewith
问题描述
loadwith和associatewith之间的区别是什么.从我阅读的文章看来,loadwith似乎用于加载附加数据(例如,客户的所有订单).而AssociateWith用于过滤数据.
what is the difference between loadwith and associatewith. From the articles i read it seems that loadwith is used to load addition data (eg all orders for the customers). While AssociateWith is used to filter data.
这是正确的理解吗?如果有人可以通过基于示例的解释对此进行解释,那也将是很好的.
Is that a correct understanding? Also it will be nice if someone can explain this with an example based explanation.
推荐答案
LoadWith
用于执行关联的渴望加载,而不是默认的惰性加载 >.
LoadWith
is used to perform an eager load of an association as opposed to the default lazy load.
通常,关联是在您第一次引用它们时加载的.这意味着,如果选择100个Order
实例,然后对它们的每个Details
进行操作,则实际上是对数据库执行101个SELECT
操作.另一方面,如果LoadOptions
指定LoadWith<Order>(o => o.Details)
,则所有操作都在添加了JOIN
的单个SELECT
中完成.
Normally, associations are loaded the first time you reference them. That means if you select 100 Order
instances, and then do something with each of their Details
, you're actually performing 101 SELECT
operations against the database. On the other hand, if the LoadOptions
specify LoadWith<Order>(o => o.Details)
, then it's all done in a single SELECT
with an added JOIN
.
AssociateWith
对加载关联的 没有任何影响,只是加载了什么.每次加载关联时,它都会添加一个WHERE
子句.
AssociateWith
doesn't have any effect on when the association is loaded, just what is loaded. It adds a WHERE
clause every time you load an association.
正如您所说,AssociateWith
用于自动 过滤数据.通常,如果您知道一个关联具有大量元素并且只需要它们的特定子集,就可以使用它.同样,它主要是性能优化,只是另一种.
As you say, AssociateWith
is used to automatically filter data. Typically you'd use this if you know that an association has a very large number of elements and you only need a specific subset of them. Again, it's mainly a performance optimization, just a different kind.
这篇关于linq to sql loadwith vs associatewith的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!