上篇文章中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. 基础002_V7-CLB

    一.综述 参考ug474.pdf: 7系列中,一个CLB包含两个slice: 每个CLB的资源: CLB可配置的主要功能: 二.主要功能 LUT是基本单元,例如选择器assign muxout =  ...

  2. talend 连接mysql数据库没有权限

    使用talend连接一个mysql数据库,提示没有权限,最后发现mysql服务器的配置中只监听了127.0.0.1的端口,拒绝非本地的请求.通过将/etc/mysql/my.cnf中的bind_add ...

  3. MySQL 5.6学习笔记(运算符)

    MySQL运算符包括四类:算术运算符.比较运算符.逻辑运算符和位运算符. 1. 算术运算符 用于种类数值运算.包括:加(+).减(-).乘(*).除(/).取余(%). 除法除数为零时,执行结果为nu ...

  4. Android基础知识之Manifest文件的组织结构

    原文:http://android.eoe.cn/topic/android_sdk 是AndroidManifest.xml文件中的根标签,她必须包含一个标签和指定的xmlns:android. p ...

  5. ISO-OSI的七层协议经典架构

    OSI(Open System interconnection)开放系统互连参考模型 ISO(International Standards Organization)国际标准化组织 第一层:物理层 ...

  6. Theories of Deep Learning

    https://stats385.github.io/readings Lecture 1 – Deep Learning Challenge. Is There Theory? Readings D ...

  7. .NET MVC+ EF+调用存储过程 多表联查以及VIEW列表显示

    直接上干活,至于网上的一大堆处理方式不予评论,做好自己的就是最好的,供大家不走弯路 1.view页面 <link href="~/Content/bootstrap.css" ...

  8. [Windows Azure] Load Balancing Virtual Machines

    Load Balancing Virtual Machines All virtual machines that you create in Windows Azure can automatica ...

  9. python *和**的用法

    1.使用场景 *和**用在函数参数列表中 2.*作函数参数 以列表的形式提供参数 def foo(*args): for arg in args: print(arg) foo(1, 2, 3) 运行 ...

  10. python(49):把文件压缩成zip格式的文件

    有时需要用到压缩文件,网上搜集了一段代码: 分享一下: import os import zipfile def make_zip(localPath, pname): zipf = zipfile. ...