Expression 动态查询的 使用小结

Linq  Expression  的动态查询使用小结, 我也是根据这个大神的例子做的,并小结http://www.cnblogs.com/lyj/archive/2008/03/25/1122157.html

命名空间 System.Linq.Expression

以查询Customer 为例 ,增加一个Where条件

 
Expression whereExpression = Expression.Call(
     typeof(Queryable), "Where",
     new Type[] { typeof(Customer) } ,
     Expression.Constant(queryCustomer), expression
 );

queryCustomer 是 IQueryable<Customer> 类型

exprssion 是  Expression<Func<Customer,bool>> 类型 ,是一个方法的参数 

Expression.Contant方法把一个数据源作为 Expression

然后就可以根据这个Expression查询

var result = queryCustomer.Provider.CreateQuery<Customer>(whereExpression);
          
foreach (var c in result)
{
     Console.WriteLine("name:{0},datetime:{1}",c.Name,c.CreatedDate);
}

这只是一个where 表达式,根据Call方法当前重载的定义可以发现,

第一个参数 typeof(Queryable) 与第二参数  "Where"

代表 调用  Queryable类 的 Where 静态方法,

第三个参数  Type[]  是 泛型方法的类型参数列表

第四个参数 Expression[] 是 传入 Where 方法(根据Call方法的第二个String参数) 的 参数


查看 Where 方法定义就可以知道大概的意思

public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);
        
public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate);

//刚才的调用,这里的参数都可以对应上Where方法的类型参数和参数
Expression whereExpression = Expression.Call(
     typeof(Queryable), "Where",
     new Type[] { typeof(Customer) } ,
     Expression.Constant(queryCustomer), expression
 );

另外再说一下OrderBy ,在刚在的where基础上再加上orderby,根据 

// c=>c.CreatedDate
ParameterExpression param = Expression.Parameter(typeof(Customer),"c");

Expression propertyExpression =Expression.Lambda(Expression.Property(param, "CreatedDate"), param);

// query.OrderBy(c=>c.CreateDate), 因为 CreatedDate 是DateTime类型,所以,要typeof(DateTime)

Expression orderbyExpression = Expression.Call( 
    typeof(Queryable),
    "OrderBy", new Type[] { typeof(Customer), typeof(DateTime) },
     whereExpression, propertyExpression 
);


//Customer 的定义
public class Customer
    {
        public virtual int ID { get; set; }
        public virtual String Name { get; set; }
        public virtual DateTime CreatedDate { get; set; }
      
    }

// 参考OrderBy定义

public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer); 

 

 

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;
using System.Linq.Expressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            var factory = cfg.Configure().BuildSessionFactory();
            var session = factory.OpenSession();
            
            TestCase3(session,x=>x.CreatedDate > DateTime.Parse("1999-1-1"));
            
            session.Close();

            Console.ReadLine();
        }


        static void TestCase3(NHibernate.ISession session,Expression<Func<Customer,bool>> expression)
        {
            
            IQueryable<Customer> queryCustomer = session.Query<Customer>();
             
            ParameterExpression param = Expression.Parameter(typeof(Customer),"cccc");

            Expression whereExpression = Expression.Call(
                typeof(Queryable), "Where",
                new Type[] { typeof(Customer) } ,
                Expression.Constant(queryCustomer), expression
            );

            Expression propertyExpression =Expression.Lambda(Expression.Property(param, "CreatedDate"), param);

          
            Expression orderbyExpression = Expression.Call(
                typeof(Queryable), "OrderBy",
                new Type[] { typeof(Customer), typeof(DateTime) },
                whereExpression,
                propertyExpression
            );

            var result = queryCustomer.Provider.CreateQuery<Customer>(orderbyExpression);
           
            foreach (var c in result)
            {
                Console.WriteLine("name:{0},datetime:{1}",c.Name,c.CreatedDate);
            }
        }

         

         
    }
}


  最后鄙视一下CSDN的前端,  HTML编辑器BUG太多了,害我改了好几次@CSDN。。。

 
 

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值