Linq表达式子级实体的n级别

时间:2013-01-30 12:05:25

标签: linq entity-framework-4 linq-to-entities linq-expressions

需要以下查询的通用表达式。

I don't want to use the Dynamic library and Predicate builder.

我正在动态生成表达式。

var test = entity.User.Where(PUser => PUser.Role.TeamRoles.Any(PTeamRoles => PTeamRoles.TeamId == "222")));

我想生成一对一的动态表达式,然后是很多关系查询。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案

Expression proeprtyexpression = Expression.Property(PTeamRoles,“TeamId”);             表达式valueexpress = Expression.Constant(“A”);             //表达式innerexpression = Expression.Lambda(Expression.Equal(proeprtyexpression,valueexpress),PTeamRoles);             表达式innerexpression = Expression.Lambda(ExcelExpressionHelper.ApplyOperator(proeprtyexpression,valueexpress,“StartsWith”),PTeamRoles);

        Type winnertype = PTeamRoles.Type;
        Type woutertype = PRole.Type;
        var wouterProperty = Expression.Property(PRole, "TeamRoles");

        var outerMethodExpression = Expression.Call(typeof(Enumerable), "Any", new[] { winnertype }, wouterProperty, innerexpression);