上篇文章中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. 【转载并整理】Linux - centOS 6 SVN服务器安装、配置及开机启动

    以前在windows上安装svn服务器,用的是VisualSVN-Server用起来还不错,但只能用在windows,在linux上部署使用是 subversion,具体说明如下 参考原文:http: ...

  2. [转]session和cookie的区别和联系,session的生命周期,多个服务部署时session管理

    Session和Cookie的区别 对象 信息量大小 保存时间 应用范围 保存位置 Session 小量,简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端 Cookie 小 ...

  3. ||在oracle数据库中起到字符串拼接的作用

    例子:select org.id from org where inner_code like '12011601001' || '%' ||在oracle数据库中起到字符串拼接的作用,上面等同于'1 ...

  4. Jacobi并行拆解【补充】

    作者:桂. 时间:2018-04-24  22:04:52 链接:http://www.cnblogs.com/xingshansi/p/8934373.html 前言 本文为Jacobi并行拆解一文 ...

  5. FIR特性及仿真实现_01

    作者:桂. 时间:2018-02-05  19:01:21 链接:http://www.cnblogs.com/xingshansi/p/8419007.html 前言 本文主要记录FIR(finit ...

  6. tomcat重启步骤

    tomcat重启步骤1.切换到tomcat目录cd /home/tomcat-home/tomcat-erp/bin2.关闭tomcatsh shutdown.sh3.启动tomcatsh start ...

  7. js获取IP地址方法总结

    js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html 1,js取得IP地址 ...

  8. FTP整站上传的批处理脚本

    一个FTP整站上传的批处理代码.例子: @echo off rem 设置FTP服务器地址 set ftpIP=192.168.0.2 rem 设置FTP用户名 set ftpUser=MyUser r ...

  9. C#.NET中遍历指定目录下的文件(及所有子目录及子目录里更深层目录里的文件)

    //遍历一个目录下所有的文件列表,代码实例 DirectoryInfo dir = new DirectoryInfo(folderName);var list = GetAll(dir); /// ...

  10. 游戏框架设计中的。绑定binding。。。命令 command 和消息message 以及MVVM

    游戏框架设计中的.绑定binding...命令 command 和消息message