Today I ran into something a bit weird.
今天我遇到了一些奇怪的事情。
I used mini-profiler to see the SQL queries executed.
我使用mini-profiler查看执行的SQL查询。
It seems that when using obj.Children.Select(x => x.Prop1).SingleOrDefault() the query executed get ALL columns. However, using ctx.Children.Select(x => x.Prop1 && x.IDParent == idObj).SingleOrDefault() gets only Prop1.
似乎在使用object . children时。选择(x => x. prop1). singleordefault()执行的查询得到所有列。然而,使用ctx.Children。选择(x = > x。Prop1 & & x。singleordefault()只获得Prop1。
Any idea why the difference?
你知道为什么会有不同吗?
1 个解决方案
#1
2
Because the first query gets executed here:
因为第一个查询在这里执行:
obj.Children. ...
And the second query gets excuted here:
第二个查询在这里被导出:
... .SingleOrDefault()
You get the first query for the full entity with all columns because the navigation property gets loaded due to lazy loading. After the entity is loaded your Select
will be applied in memory. The database query is already finished at this point when you project to the single property.
您将获得包含所有列的完整实体的第一个查询,因为导航属性由于延迟加载而被加载。加载实体之后,您的选择将被应用到内存中。当您向单个属性进行项目时,此时数据库查询已经完成。
The second query performs the projection (selecting a single column) really in the database and does not load more than this column.
第二个查询在数据库中执行投影(选择一个列),并且不会加载比这个列更多的内容。
#1
2
Because the first query gets executed here:
因为第一个查询在这里执行:
obj.Children. ...
And the second query gets excuted here:
第二个查询在这里被导出:
... .SingleOrDefault()
You get the first query for the full entity with all columns because the navigation property gets loaded due to lazy loading. After the entity is loaded your Select
will be applied in memory. The database query is already finished at this point when you project to the single property.
您将获得包含所有列的完整实体的第一个查询,因为导航属性由于延迟加载而被加载。加载实体之后,您的选择将被应用到内存中。当您向单个属性进行项目时,此时数据库查询已经完成。
The second query performs the projection (selecting a single column) really in the database and does not load more than this column.
第二个查询在数据库中执行投影(选择一个列),并且不会加载比这个列更多的内容。