EF Code First - Include(x => x.Properties.Entity) a 1 : Many association

后端 未结 3 1817
借酒劲吻你
借酒劲吻你 2020-12-13 18:08

Given a EF-Code First CTP5 entity layout like:

public class Person { ... }

which has a collection of:

public class Address {

相关标签:
3条回答
  • 2020-12-13 18:15

    For that you can use the Select method:

    PersonQuery.Include(x => x.Addresses.Select(a => a.Mailbox));
    

    You can find other examples in here and here.

    0 讨论(0)
  • 2020-12-13 18:21

    It is described in this post: http://www.thomaslevesque.com/2010/10/03/entity-framework-using-include-with-lambda-expressions/

    Edit (By Asker for readability): The part you are looking for is below:

    public static class ObjectQueryExtensions
    {
        public static ObjectQuery<T> Include<T>(this ObjectQuery<T> query, Expression<Func<T, object>> selector)
        {
            string path = new PropertyPathVisitor().GetPropertyPath(selector);
            return query.Include(path);
        }
    
        class PropertyPathVisitor : ExpressionVisitor
        {
            private Stack<string> _stack;
    
            public string GetPropertyPath(Expression expression)
            {
                _stack = new Stack<string>();
                Visit(expression);
                return _stack
                    .Aggregate(
                        new StringBuilder(),
                        (sb, name) =>
                            (sb.Length > 0 ? sb.Append(".") : sb).Append(name))
                    .ToString();
            }
    
            protected override Expression VisitMember(MemberExpression expression)
            {
                if (_stack != null)
                    _stack.Push(expression.Member.Name);
                return base.VisitMember(expression);
            }
    
            protected override Expression VisitMethodCall(MethodCallExpression expression)
            {
                if (IsLinqOperator(expression.Method))
                {
                    for (int i = 1; i < expression.Arguments.Count; i++)
                    {
                        Visit(expression.Arguments[i]);
                    }
                    Visit(expression.Arguments[0]);
                    return expression;
                }
                return base.VisitMethodCall(expression);
            }
    
            private static bool IsLinqOperator(MethodInfo method)
            {
                if (method.DeclaringType != typeof(Queryable) && method.DeclaringType != typeof(Enumerable))
                    return false;
                return Attribute.GetCustomAttribute(method, typeof(ExtensionAttribute)) != null;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 18:38

    For any one thats still looking for a solution to this, the Lambda includes is part of EF 4+ and it is in the System.Data.Entity namespace; examples here

    http://romiller.com/2010/07/14/ef-ctp4-tips-tricks-include-with-lambda/

    0 讨论(0)
提交回复
热议问题