25

It is working code;

IQueryable<Product> productQuery = ctx.Set<Product>().Where(p => p.Id == id).(Include"Contexts.AdditionalProperties.Field");

But you know that it could not produce compile time error if we made mistake in string statement in "Contexts.AdditionalProperties.Field"

I would like to write code below;

IQueryable<Product> productQuery = ctx.Set<Product>().Where(p => p.Id == id).Include(p => p.Contexts);

But above statement could not give chance to define AdditionalProperties and Field.

What should we do?

I would like to write as more than one include for build query.

Thanks.

Nuri YILMAZ
  • 4,291
  • 5
  • 37
  • 43

1 Answers1

43

If AdditionalProperties is a single reference to another object:

using System.Data.Entity;
...
IQueryable<Product> productQuery = ctx.Set<Product>()
        .Include(p => p.Contexts.AdditionalProperties.Field)
        .Where(p => p.Id == id);


If AdditionalProperties is a collection then you can use the Select method:

IQueryable<Product> productQuery = ctx.Set<Product>()
        .Include(p => p.Contexts.AdditionalProperties.Select(a => a.Field))
        .Where(p => p.Id == id);

Don't forget to import System.Data.Entity namespace in your class file!

Morteza Manavi
  • 33,026
  • 6
  • 100
  • 83
  • I have to tell you that it was *not* a fair game... Since I answered this very question a few months ago here. Sorry dude! – Morteza Manavi Jan 20 '11 at 19:13
  • Thanks for fast answer! How can I add another where contion for AdditionalProperties which is a List<> type object? – Nuri YILMAZ Jan 20 '11 at 21:19
  • 3
    You can't. Include does not let conditional loading on the navigation properties. For that you need to eliminate the Include and load the filtered navigation properties by a *Filtered Projection* with Anonymous types. Take a look at this thread: http://stackoverflow.com/questions/3718400/conditional-eager-loading/3718457#3718457 – Morteza Manavi Jan 20 '11 at 21:30