Where did the lambda-accepting overload of DbQuery.Include() go?

c# entity-framework


I recently defined several code-first models for an EntityFramework-based new project.

public class BlogEntry
    public long Id { get; set; }
    public long AuthorId { get; set; }
    public DateTime PublishedStamp { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }

    public virtual User Author { get; set; }

public class User
    public long Id { get; set; }
    public string Email { get; set; }
    // ...

class BlogDb : DbContext
    public DbSet<BlogEntry> Entries { get; set; }
    public DbSet<User> Users { get; set; }

Let's say I want to get the 10 most recent blog posts:

var entries = new BlogDb().Entries.OrderByDescending(...).Take(10).ToList();

Today's issue is that accessingentry.Author will lead to an additional database query. You wouldn't want to create a different such query for each blog post. As of right now, I believe that the aim ofInclude precisely this situation, thus I may say:

var entries = new BlogDb().Entries.Include(e => e.Author).(...).ToList();

However, it doesn't seem that there is such a technique. There is only oneInclude(string) such like this:

var entries = new BlogDb().Entries.Include("Author").(...).ToList();

However, this is irritating since the rename refactoring will miss it because it is not verified at build time. Obviously, the "right" method is the one that uses the lambda.

What happened to that approach? Is it still a part of EntityFramework?

(I can accomplish this for myself by writing an extension method, so you don't have to. Just to make sure I'm not missing anything, please.)

3/25/2012 12:38:04 AM

Accepted Answer

using System.Data.Entity;

It's in EF v4.1 and later, however as it's an extension function, you'll need a reference.

EditIn appreciation of @EastonJamesHarvey

If Core EF is being used, the import should be:

using Microsoft.EntityFrameworkCore;
11/14/2018 3:35:43 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow