39

I am using entity framework and I can't find include method like in this example:

using(ArticleExtractorEntities db=new ArticleExtractorEntities())  
{
    Preference pref= db.Preferences.Include(  

here i find only the function include with the parameter (string path) and I don't find any other overload so how can I use Include with lambda expression?

1
  • 1
    You need to be using EF 4 .1 or later. Note that what ships with VS 2010, is EF 4, which doesn't have this. Commented Jun 29, 2012 at 9:48

4 Answers 4

98

it's not in System.Linq. Add

using System.Data.Entity
Sign up to request clarification or add additional context in comments.

2 Comments

@ahmadhori you're in linq to entities, or linq to sql ?
Since the latest updates, this has been moved. Please see my answer below if you've upgraded your framework.
12

Update. For those looking how it extend your linq query with .Include()

No longer is it:

using System.Data.Entity;

With netcoreapp1.0 it is:

using Microsoft.EntityFrameworkCore;

1 Comment

And that's not just for those using "EF Core?" (or ".Net Core" if you will)
1

You could implement it as shown in this blog post:

public static class ObjectQueryExtension
{
    public static ObjectQuery<T> Include<T>(this ObjectQuery<T> mainQuery, Expression<Func<T, object>> subSelector)
    {
        return mainQuery.Include(FuncToString(subSelector.Body));
    }
    private static string FuncToString(Expression selector)
    {
        switch (selector.NodeType)
        {
            case ExpressionType.MemberAccess:
                return ((selector as MemberExpression).Member as Reflection.PropertyInfo).Name;
            case ExpressionType.Call:
                var method = selector as MethodCallExpression;
                return FuncToString(method.Arguments[0]) + "." + FuncToString(method.Arguments[1]);
            case ExpressionType.Quote:
                return FuncToString(((selector as UnaryExpression).Operand as LambdaExpression).Body);
        }
        throw new InvalidOperationException();
    }
    public static K Include<T, K>(this EntityCollection<T> mainQuery, Expression<Func<T, object>> subSelector)
        where T : EntityObject, IEntityWithRelationships
        where K : class
    {
        return null;
    }
    public static K Include<T, K>(this T mainQuery, Expression<Func<T, object>> subSelector)
        where T : EntityObject
        where K : class
    {
        return null;
    }
}

Comments

0

Look at the following articles:

http://www.thomaslevesque.com/2010/10/03/entity-framework-using-include-with-lambda-expressions/

http://msmvps.com/blogs/matthieu/archive/2008/06/06/entity-framework-include-with-func-next.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.