【发布时间】:2012-06-26 10:33:15
【问题描述】:
我正在使用 Linq2Sql(我的客户卡在 3.5,所以我无法迁移到实体框架)来访问 SQL Server 数据库。
为了在某些情况下提高性能,我已将 LoadOptions 附加到我的上下文中。 当我使用编译查询时,我无法禁用它,当它们无用并且它们减慢请求时也是如此。
但有时我想检索数据,就像我的上下文中没有附加 LoadOptions 一样。
作为一种解决方法,我尝试返回的不是完整记录,而是它的投影。
例子:
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Product>(c => c.X);
dlo.LoadWith<Product>(c => c.Y);
context.LoadOptions = dlo;
return (from product in context.Products
where ...
select product).First();
这会执行如下查询:
Select product.*, X.*, Y.* from Product Left outer join X left outer join Y where....
在这种情况下,一切都很正常。
我的方法依赖于这样的东西:
return (from product in context.Products
where ...
select new MyType() { p = product.Field }).First();
执行类似
的查询Select product.Field from Product ->Left outer join X left outer join
Y<-- where....
请注意请求中的 LEFT OUTER JOIN。
虽然我期待类似的东西:
Select product.Field from Product where....
所以我想知道是否有办法避免这些连接?
非常感谢您的建议,
【问题讨论】:
-
表 X 和 Y 与表 Product 有什么关系? X 和 Product 以及 Y 和 Product 之间是否存在外键关系?
-
是的,产品的外键在 X et Y 中。
标签: c# .net linq linq-to-sql orm