C# 如何构造 LINQ to Entities 查询以直接加载子对象,而不是调用 Reference 属性或 Load()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/315966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
提问by JC Grubbs
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it) and I'm writing a lot of code like this:
我刚开始使用 LINQ to Entities(或 Entity Framework,无论他们怎么称呼它),我正在编写很多这样的代码:
var item = (from InventoryItem item in db.Inventory
where item.ID == id
select item).First<InventoryItem>();
and then calling methods on that object like this:
然后像这样调用该对象上的方法:
var type = item.ItemTypeReference;
or
或者
var orders = item.OrderLineItems.Load();
to retrieve child or related objects.
检索子对象或相关对象。
I haven't profiled the DB or dug too deeply but my guess is that when I call a .Load() or a *Reference property I'm actually making another call to the DB. If this is the case, is there any way to get those objects in my initial LINQ expression?
我没有对 DB 进行概要分析或挖掘得太深,但我的猜测是,当我调用 .Load() 或 *Reference 属性时,我实际上是在再次调用 DB。如果是这种情况,有没有办法在我的初始 LINQ 表达式中获取这些对象?
采纳答案by Robert Wagner
You want to use the .Include(string) method references in this "Shaping query results"article.
您想使用这篇“塑造查询结果”文章中的 .Include(string) 方法引用。
var item = from InventoryItem item in
db.Inventory.Include("ItemTypeReference").Include("OrderLineItems")
where item.ID == id
select item;
There is probably a "sql" style syntax for the Includes as well.
Includes 也可能有“sql”样式的语法。
Also see this articleabout moving from LINQ-to-SQL to LINQ-to-Entities.
另请参阅该文章有关从LINQ到SQL移动到LINQ到实体。
For others looking for a solution to this problem for Linq to SQLyou want to do the following (Substitute DataContext and other types for whatever you have):
对于为Linq to SQL寻找此问题的解决方案的其他人,您需要执行以下操作(将 DataContext 和其他类型替换为您拥有的任何类型):
using (DataContext db = new DataContext())
{
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<InventoryItem>(ii => ii.ItemTypeReference);
options.LoadWith<InventoryItem>(ii => ii.OrderLineItems);
db.LoadOptions = options;
var item = from InventoryItem item in db.Inventory
where item.ID == id
select item;
}
This will load the properties specified in LoadWith whenever the parent item (InventoryItem) is loaded, for that particular context.
这将在加载父项 (InventoryItem) 时加载 LoadWith 中指定的属性,用于该特定上下文。
In response to some further questions from James and Jesper, check out this question
在回答 James 和 Jesper 的一些进一步问题时,请查看这个问题
回答by Mike Chamberlain
In addition to Robert's answer, you might like to check out this question for options for an extension method that that allows you to .Include() using an expression instead of a string, so you get compile time checking:
除了 Robert 的回答之外,您可能还想查看此问题以了解扩展方法的选项,该方法允许您使用表达式而不是字符串 .Include() ,以便您进行编译时检查:
回答by Davut Gürbüz
AFAIK, For silverlight(domain services) adding [Include] attribute to right place(over navigation property in metadata) is enough https://stackoverflow.com/a/5332188/413032
AFAIK,对于 Silverlight(域服务)添加 [Include] 属性到正确的位置(元数据中的导航属性)就足够了https://stackoverflow.com/a/5332188/413032