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
Ext.ux.grid.feature.Searching 解析查询参数,动态产生linq lambda表达式的更多相关文章
- Extjs4.2 Grid搜索Ext.ux.grid.feature.Searching的使用
背景 Extjs4.2 默认提供的Search搜索,功能还是非常强大的,只是对于国内的用户来说,还是不习惯在每列里面单击好几下再筛选,于是相当当初2.2里面的搜索,更加的实用点,于是在4.2里面实现. ...
- 将Lambda表达式作为参数传递并解析-在构造函数参数列表中使用Lambda表达式
public class DemoClass { /// <summary> /// 通过Lambda表达式,在构造函数中赋初始值 /// </summary> /// < ...
- 函数-->指定函数--->默认函数--->动态函数--> 动态参数实现字符串格式化-->lambda表达式,简单函数的表示
#一个函数何以接受多个参数#无参数#show(): ---> 执行:show() #传入一个参数 def show(arg): print(arg) #执行 show(123) #传入两个参数 ...
- Solr4:查询参数fq的用法(对结果进行过滤;两组关键词组合查询)
Solr查询参数文档可以参考: http://wiki.apache.org/solr/CommonQueryParameters#head-6522ef80f22d0e50d2f12ec487758 ...
- node.js----一个httpserver提交和解析get参数的例子
前端代码 <!doctype html> <html lang="en"> <head> <meta charset="utf- ...
- C# LINQ查询表达式用法对应Lambda表达式
C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...
- ASP.NET EF(LINQ/Lambda查询)
EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...
- 完善ext.grid.panel中的查询功能(紧接上一篇)
今天的代码主要是实现,Ext.grid.panel中的查询,其实我也是一名extjs新手,开始想的实现方式是另外再创建一个新的grid类来存放查询出的数据(就是有几个分类查询就创建几个grid类),这 ...
- Node基础:url查询参数解析之querystring
模块概述 在nodejs中,提供了querystring这个模块,用来做url查询参数的解析,使用非常简单. 模块总共有四个方法,绝大部分时,我们只会用到 .parse(). .stringify() ...
随机推荐
- jQuery的prop和attr的区别,及判断复选框是否选中
jQuery的prop和attr的区别 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. 参数有区别,att ...
- mysql分享一:运维角度浅谈MySQL数据库优化
转于:http://lizhenliang.blog.51cto.com/7876557/1657465 1.数据库表设计要合理避免慢查询.低效的查询语句.没有适当建立索引.数据库堵塞(死锁)等 2. ...
- hudson用SVN插件下载代码,用ant插件打包, 用SSH插件部署
hudson自动化部署步骤 1.SVN插件->下载代码 2.ant插件->war打包 (hudson服务器上可安装多个版本ant,每个项目可以选择一个ant版本.Build File ...
- PHP数组排序sort、asort与ksort用法
分享下PHP数组排序之sort.asort与ksort用法,实例中简单示范了sort.asort与ksort的用法,并备有注释加以详细说明. PHP数组排序中sort.asort与ksort的用法. ...
- 一条SQL语句中算日销售额和月销售额
刚刚做项目的时候用到的 用户表:用户ID,用户名,余额 流水表:时间,用户ID,用户名,类型(0充值,1消费),变更金额 现在要查每个用户的日销售额和月销售额,本来最简单的方法是先把所有用户查出来,然 ...
- sqlite第三方类库:FMDB使用
转自:http://www.cnblogs.com/wuhenke/archive/2012/02/07/2341656.html 本文转自一位台湾ios开发者的blog,由于blog地址被墙掉,转发 ...
- UML的通用机制(三)
Common Divisions In modeling object-oriented systems, the world often gets divided in several way ...
- c# 除掉前三个字符,剩下的4个字符全为数字方为特殊车辆
string plate="粤BN1223"; if (plate.Contains("粤BN")) { //除掉前三个字符,剩下的4个字符全为数字方为特殊车辆 ...
- C++技术沙龙报名开始啦!
沙龙主题:C++甜点关键字:C++之美,黑科技,神奇和魔力内容:三场主题演讲和一场开放性话题讨论时间:2015年5月16日下午2:00-6:00地点:珠海金山办公软件1楼VIP厅,珠海市吉大景山路莲山 ...
- (转)Go和HTTPS
转自:http://studygolang.com/articles/2946 Go和HTTPS 2015-04-30 bigwhite 阅读 5688 次 4 人喜欢 3 条评论 收 ...