动态表达式

http://www.cnblogs.com/blusehuang/archive/2007/07/13/816970.html

 

 

  ParameterExpression c = Expression.Parameter(typeof(Customer), "c");

 Expression condition = Expression.Constant(True);
 if(srtring.sinullorempty(S))
    Expression con = Expression.Call(
         Expression.Property(c, typeof(Customer).GetProperty("CustomerID")),
         typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }),
         Expression.Constant(s));
     condition = Expression.And(con, condition);
 }
 Expression<Func<Customer, bool>> end =
     Expression.Lambda<Func<Customer, bool>>(condition, new ParameterExpression[] { c });
 
 http://kb.cnblogs.com/page/42711/2/
   //如果不为null才算做条件
                        if ( p.GetValue(obj, null!= null)
                        
{
                            ParameterExpression param 
= Expression.Parameter(typeof(TEntity), "c");
                            Expression right 
= Expression.Constant(p.GetValue(obj, null));
                            Expression left 
= Expression.Property(param, p.Name);
                            Expression filter 
= Expression.Equal(left,right);

                            Expression
<Func<TEntity, bool>> pred = Expression.Lambda<Func
<
TEntity, bool>>(filter, param);
                            query 
= query.Where(pred);
                        }

                    }

 

 

 


转载于:https://www.cnblogs.com/ruyi/archive/2012/03/13/2394302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值