动态表达式

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);
                        }

                    }

 

 

 


posted @ 2012-03-13 18:34  Ry5  阅读(444)  评论(1编辑  收藏  举报