【问题标题】:Lambda Expresssion - using EntityFramework Include() and Any operation?Lambda 表达式 - 使用实体框架 Include() 和操作?
【发布时间】:2012-10-22 20:21:24
【问题描述】:

以下代码正在运行

var queryResults = _db.Projects
           .Include("Participants.Person")
           .Where(Project => Project.Participants.Any(Parti => Parti.Person.FirstName == "test3"));

我正在动态构建 lambda 表达式。为了实现上述目标,我必须编写大量代码。

我想实现以下目标。

var queryResults = _db.Projects
           .Include("Participants.Person")
           .Where(Project => Project.Participants.Person.FirstName == "test3"));

任何建议请分享。

修改后的部分

我正在尝试任何操作。但我在这条线上遇到了例外。有什么建议吗?

MemberExpression propertyOuter = Expression.Property(c, "Participant");

ParameterExpression tpe = Expression.Parameter(typeof(Participant), "Participant");
Expression left1 = Expression.Property(tpe, typeof(Participant).GetProperty("Person"));
Expression left2 = Expression.Property(left1, typeof(Person).GetProperty("FirstName"));
Expression right1 = Expression.Constant(filter.FieldValue);
Expression InnerLambda = Expression.Equal(left2, right1);
Expression<Func<Participant, bool>> innerFunction = Expression.Lambda<Func<Participant, bool>>(InnerLambda, tpe);

MethodInfo method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(Participant));

MemberExpression propertyOuter = Expression.Property(c, "Participant");

var anyExpression = Expression.Call(method, propertyOuter, innerFunction);

【问题讨论】:

    标签: lambda entity-framework-5


    【解决方案1】:

    也许这对你有用。

    var queryResults = _db.Persons
       .Where( p => p.FirstName == "test3")
       .SelectMany(p => p.Participant.Projects)
       .Include("Participants.Person"); 
    

    编辑:或者如果一个人有很多参与者

    var queryResults = _db.Persons
       .Where( p => p.FirstName == "test3")
       .SelectMany(p => p.Participants.SelectMany(par => par.Projects))
       .Include("Participants.Person"); 
    

    【讨论】:

    • 不,我在这一行没有找到子实体 .SelectMany(p => p.Participants.)
    • 您是否按照建议从 _db.Persons 中选择或从 _db.Projects 中选择?
    • 我只从 _db.Persons 中选择。
    • 好吧,这意味着人和参与者处于 M 对 N 关系,对吧?
    • 查看已编辑的建议。 ...SelectMany(p => p.Participants.SelectMany(par => par.Projects))...
    【解决方案2】:

    我得到了解决方案,它的工作原理。

    Lambda 表达式

    var queryResults = _db.Projects
           .Include("Participants.Person")
           .Where(Project => Project.Participants.Any(Participant => Participant.Person.FirstName == "test3"));
    

    构建动态 lambda 表达式的源代码

     ParameterExpression c = Expression.Parameter(typeof(T), entityType.Name);
     ParameterExpression tpe = Expression.Parameter(typeof(Participant), "Participant");
     Expression left1 = Expression.Property(tpe, typeof(Participant).GetProperty("Person"));
     Expression left2 = Expression.Property(left1, typeof(Person).GetProperty("FirstName"));
     Expression right1 = Expression.Constant(filter.FieldValue);
     Expression InnerLambda = Expression.Equal(left2, right1);
     Expression<Func<Participant, bool>> innerFunction = Expression.Lambda<Func<Participant, bool>>(InnerLambda, tpe);
    
     MethodInfo method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(Participant));
    
     var outer = Expression.Property(c, typeof(Project).GetProperty("Participants"));
    
     var anyExpression = Expression.Call(method, outer, innerFunction);
    

    这很有帮助。 Building a dynamic expression tree to filter on a collection property

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2014-10-09
      • 2011-03-30
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多