上篇文章中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

Ext.ux.grid.feature.Searching 解析查询参数,动态产生linq lambda表达式的更多相关文章

  1. Extjs4.2 Grid搜索Ext.ux.grid.feature.Searching的使用

    背景 Extjs4.2 默认提供的Search搜索,功能还是非常强大的,只是对于国内的用户来说,还是不习惯在每列里面单击好几下再筛选,于是相当当初2.2里面的搜索,更加的实用点,于是在4.2里面实现. ...

  2. 将Lambda表达式作为参数传递并解析-在构造函数参数列表中使用Lambda表达式

    public class DemoClass { /// <summary> /// 通过Lambda表达式,在构造函数中赋初始值 /// </summary> /// < ...

  3. 函数-->指定函数--->默认函数--->动态函数--> 动态参数实现字符串格式化-->lambda表达式,简单函数的表示

    #一个函数何以接受多个参数#无参数#show(): ---> 执行:show() #传入一个参数 def show(arg): print(arg) #执行 show(123) #传入两个参数 ...

  4. Solr4:查询参数fq的用法(对结果进行过滤;两组关键词组合查询)

    Solr查询参数文档可以参考: http://wiki.apache.org/solr/CommonQueryParameters#head-6522ef80f22d0e50d2f12ec487758 ...

  5. node.js----一个httpserver提交和解析get参数的例子

    前端代码 <!doctype html> <html lang="en"> <head> <meta charset="utf- ...

  6. C# LINQ查询表达式用法对应Lambda表达式

    C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...

  7. ASP.NET EF(LINQ/Lambda查询)

    EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...

  8. 完善ext.grid.panel中的查询功能(紧接上一篇)

    今天的代码主要是实现,Ext.grid.panel中的查询,其实我也是一名extjs新手,开始想的实现方式是另外再创建一个新的grid类来存放查询出的数据(就是有几个分类查询就创建几个grid类),这 ...

  9. Node基础:url查询参数解析之querystring

    模块概述 在nodejs中,提供了querystring这个模块,用来做url查询参数的解析,使用非常简单. 模块总共有四个方法,绝大部分时,我们只会用到 .parse(). .stringify() ...

随机推荐

  1. Java 8 – How to sort a Map

    Java 8 – How to sort a Map 1. Quick ExplanationMap result = map.entrySet().stream() .sorted(Map.Entr ...

  2. 使用maven生成可执行的jar包

    从pom的xsi中可以打开描述pom的schema: 可以看到pom中,project的结构: 默认的mvn install生成的jar是不带主类入口的,需要在maven-compile-plugin ...

  3. 在Windows系统上怎么使用SecureCRT链接Linux AWS EC2 -摘自网络

    在Windows系统上就需要使用SecureCRT,Putty等工具,进行连接.但是AWS提供的XXX.pem文件,需要做一些处理SecureCRT的方法: 1.使用XXX.pem文件生成一个公钥文件 ...

  4. php分享十二:分组取前N记录

    经常看到问题,如何取出每组的前N条记录 http://blog.csdn.net/acmain_chm/article/details/4126306 问题:有表 如下,要求取出各班前两名(允许并列第 ...

  5. Android app 全局异常统一处理

    异常处理需求 Android app 出现 crash 时,会出现 "程序异常退出" 的提示并关闭,体验不好,另外主要是无法知道哪里出现的崩溃,需要知道哪里造成的异常,就需要一个全 ...

  6. 【Unity】9.1 导入粒子系统组件

    分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 Unity 5.x已经自带了粒子系统组件,新建工程后只需要直接导入它就可以使用了. 二.导入Unity自带的粒子系统组件 ...

  7. 细说php(二) 变量和常量

    一. 变量的声明 1. php中声明变量的方法:  $var = 123; 2. php是弱类型的语言, 变量的类型由存储的值决定 注: Java是强类型语言, 声明变量一定要先指定类型 二. 变量的 ...

  8. C和指针第13章第4题

    题目:编写一个函数,它用于对一个任何类型的数组进行排序. 算法核心代码sort函数实现 /** 功能说明:sort函数可以对不同类型的数据进行排序 参数: 1.一个指向需要排序的数组的第一个值的指针. ...

  9. 解决failed to get the required adt version from sdk version

    在网上看了很多,选择其中的一个解决方法试了下, 还行. AS 2.3之后不能和Eclipse共用一个SDK,给Eclispe重新配置一个SDK路径

  10. Redis source code analysis

    http://zhangtielei.com/posts/blog-redis-dict.html http://zhangtielei.com/assets/photos_redis/redis_d ...