Ext.ux.grid.feature.Searching 解析查询参数,动态产生linq lambda表达式

上篇文章中http://www.cnblogs.com/qidian10/p/3209439.html我们介绍了如何使用Grid的查询组建,而且将查询的参数传递到了后台。

那么我们后台如何介绍参数,并且转换为EntityFramework的条件呢?

首先我们获取Ext.ux.grid.feature.Searching的参数,上篇文章中我们很容易发现,查询传递到后台的是fields和query参数,其中fields是参加查询的列数组,query是关键字。

首先我们定义个类,接收参数

namespace ElegantWM.EntityModel
{
    public class ExtGridSearch
    {
        public string[] fields { get; set; }
        public string query { get; set; }
    }
}
public JsonResult GET(int start, int limit, ExtGridSearch condition)
        {
            //condition提交到bll层转换
        }
//将eSearch转换为标准的linq查询
            if (eSearch != null && eSearch.fields != null && !string.IsNullOrEmpty(eSearch.query))
            {
                Expression<Func<T, bool>> Conditions = ConvertExtSearch2Linq.Convert<T>(eSearch.fields, eSearch.query);
            }
/*******************************/
/*    关键的转换类                                    */
/*******************************/

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

namespace ElegantWM.Tools
{
    public class ConvertExtSearch2Linq
    {
        public static Expression<Func<T, bool>> Convert<T>(string[] columns, string query)
        {
            Expression<Func<T, bool>> Conditions = PredicateExtensions.False<T>();
            ParameterExpression param = Expression.Parameter(typeof(T), "t");
            foreach (string col in columns)
            {
                //Expression left = Expression.Property(param, typeof(T).GetProperty(col));
                //Expression filter = Expression.Equal(left, right);
                PropertyInfo propertyInfo = typeof(T).GetProperty(col);
                if (propertyInfo == null) continue;
                //构造左右表达式
                Expression left = Expression.Property(param,propertyInfo);
                Expression right = Expression.Constant(query);
                Expression filter;
                if (propertyInfo.PropertyType.Name == "Guid")
                {
                    Guid Id;
                    if (!Guid.TryParse(query, out Id)) continue;
                    filter = Expression.Equal(left, Expression.Constant(Id));
                }
                else
                {
                    filter = Expression.Call(left,
                                                   typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
                                                   right);
                }
                if (filter == null) continue;
                Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(filter, param);
                Conditions = Conditions.Or<T>(lambda);
            }
            return Conditions;
        }
    }
}

ok,提交转换好的Conditions到EF的Where即可

参考文献:

http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet

http://www.cnblogs.com/songsh96/archive/2009/02/19/1393685.html

http://www.cnblogs.com/daviddai/archive/2013/03/09/2952087.html

http://www.cnblogs.com/coolcode/archive/2009/09/28/IQueryBuilder.html

posted @ 2013-07-23 21:40  jackchain  阅读(698)  评论(0编辑  收藏  举报