上篇文章中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. JavaScript 如何从引用类型(Array 、 Object)创建一个新的对象

    数组的增删改 1.新增一项可以使用concat方法,它不会对原有数组进行改动,而是创建一个新数组 let a = [0, 1, 2] let b = a.concat([3]) console.log ...

  2. 设置eclipse/myeclipse的智能提示

    打开eclipse/myeclipse选择 window-->Preferences-->JAVA-->Editor-->单击Content Assist–>在右边找到A ...

  3. Django form入门详解--1

     form在django中的作用: 1.可以用于自动生成form的html 2.数据校验 3.与model一在一起使用.可以大的方便数据驱动型网站的开发 编程中有许多的东西是“不可描述”的.只有动手去 ...

  4. Linux 系统结构详解【转】

    Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统.部分层次结构如图1-1所 ...

  5. macbook中vagrant升级新版本

    macbook由于缺少卸载机制,有时候不知道该如何升级软件. vagant的升级到时简单,经测试,只需直接官网下载新软件,安装即可. 旧版本不用管,新的会直接替换.

  6. mysql trigger 触发器

    创建触发器: CREATE [DEFINER = {user|CURRENT_USER}] TRIGGER trigger_name trigger_time trigger_event ON tbl ...

  7. zabbix 对网卡的流量的监控

    新建Template:Network incoming or outcoming on eth1 新建items:Network incoming on eth1 特别注意:储存值:差量(每秒速率)- ...

  8. 行为类模式(二):命令(Command)

    定义 将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能. UML 优点 能比较容易的设计一个命令队列 可以较容易的将命令加入日志 ...

  9. CNN卷积可视化与反卷积

    1.<Visualizing and Understanding Convolutional Networks> 2.<Adaptive deconvolutional networ ...

  10. (原创)拨开迷雾见月明-剖析asio中的proactor模式(一)

    使用asio之前要先对它的设计思想有所了解,了解设计思想将有助于我们理解和应用asio.asio是基于proactor模式的,asio的proactor模式隐藏于大量的细节当中,要找到它的踪迹,往往有 ...