zoukankan      html  css  js  c++  java
  • 使用Entity Framework时动态生成lamda表达式

    public Expression<Func<Job, bool>> ToLambda()
            {
                Type type = typeof (Job);
                ParameterExpression parameterExpression = Expression.Parameter(type, "job");

                Expression body = Expression.Equal(Expression.Property(parameterExpression, "MemberId"), Expression.Constant(MemberId));

                if (Key.IsNotNullAndWhiteSpace())
                {
                    MemberExpression keyMember = Expression.Property(parameterExpression, "JobName");
                    Expression value = Expression.Constant(Key);
                    MethodCallExpression keyExpression = Expression.Call(keyMember,
                                                                              typeof (string).GetMethod("Contains"),
                                                                              value);
                    body = Expression.And(body, keyExpression);
                }

                if(IsOpen.HasValue)
                {
                    MemberExpression isOpenExpression = Expression.Property(parameterExpression, "IsOpen");
                    Expression openExpression = Expression.Equal(isOpenExpression, Expression.Constant(IsOpen));
                    body = Expression.And(body, openExpression);
                }

                return Expression.Lambda<Func<Job, bool>>(body, parameterExpression);
            }

  • 相关阅读:
    组织过程资产
    事业环境因素
    一起来学习Android自定义控件2-简单的写字板控件
    Android自定义控件1
    一起来学习Android自定义控件1
    Java你可能不知道的事(3)HashMap
    Java你可能不知道的事(3)HashMap
    Java你可能不知道的事(3)HashMap
    java你可能不知道的事(2)--堆和栈
    java你可能不知道的事(2)--堆和栈
  • 原文地址:https://www.cnblogs.com/hyl8218/p/2443316.html
Copyright © 2011-2022 走看看