Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF4 LINQ Include(string) alternative to hard-coded string?

Is there any alternative to this:

Organizations.Include("Assets").Where(o => o.Id == id).Single()

I would like to see something like:

Organizations.Include(o => o.Assets).Where(o => o.Id == id).Single()

to avoid the hard-coded string "Assets".

like image 221
Steve Macdonald Avatar asked Apr 06 '10 16:04

Steve Macdonald


1 Answers

In EF 4.1, there is a built-in extension method for this.

You must have a reference to "EntityFramework" assembly (where EF 4.1 lives) in your project and use System.Data.Entity.

using System.Data.Entity; 

If you want to include nested entities, you do it like this:

        db.Customers.Include(c => c.Orders.Select(o => o.LineItems))

Not sure if this works for EF4.0 entities (ObjectContext based).

like image 116
Kasey Speakman Avatar answered Oct 20 '22 17:10

Kasey Speakman