Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()

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:

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?

like image 557
JC Grubbs Avatar asked Nov 25 '08 00:11

JC Grubbs


People also ask

How do you apply LINQ to an object?

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML. You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>.

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

What is LINQ to entity in C#?

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.


1 Answers

You want to use the .Include(string) method references in this "Shaping query results" article.

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.

Also see this article about moving from LINQ-to-SQL to LINQ-to-Entities.

For others looking for a solution to this problem for Linq to SQL you want to do the following (Substitute DataContext and other types for whatever you have):

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.

In response to some further questions from James and Jesper, check out this question

like image 106
Robert Wagner Avatar answered Oct 09 '22 12:10

Robert Wagner