ahjesus动态生成表达式树
直接上方法,看的懂的拿去用,看不懂的找资料看懂
public PartialViewResult _Product(int pageindex = , int pagesize = , Double floorprice = , Double topprice = , string brandstr = "", string categorystr = "", string orderBy = "priceasc") {
int[] brands;
if (string.IsNullOrWhiteSpace(brandstr)) {
brands = null;
}
else {
brands = Array.ConvertAll<string, int>(brandstr.Split(','), delegate(string s) { return int.Parse(s); });
}
int[] categorys;
if (string.IsNullOrWhiteSpace(categorystr)) {
categorys = null;
}
else {
categorys = Array.ConvertAll<string, int>(categorystr.Split(','), delegate(string s) { return int.Parse(s); });
}
IEnumerable<Product> product; ParameterExpression paramExpr = Expression.Parameter(typeof(Product), "it"); MemberExpression floorpricePropExpr = Expression.Property(paramExpr, "UnitPrice");
ConstantExpression floorpriceValueExpr = Expression.Constant(floorprice, typeof(Double));
BinaryExpression floorpriceExpr = Expression.GreaterThanOrEqual(floorpricePropExpr, floorpriceValueExpr);
//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
MemberExpression toppricePropExpr = Expression.Property(paramExpr, "UnitPrice");
ConstantExpression toppriceValueExpr = Expression.Constant(topprice, typeof(Double));
BinaryExpression toppriceExpr = Expression.LessThanOrEqual(toppricePropExpr, toppriceValueExpr); Expression whereExpr = Expression.And(floorpriceExpr, toppriceExpr);
Expression whereBrandExpr = null;
if (brands != null && brands.Length > ) {
for (int i = , j = brands.Length; i < j; i++) {
int brand = brands[i];
MemberExpression BrandPropExpr = Expression.Property(paramExpr, "Brand");
ConstantExpression BrandValueExpr = Expression.Constant(brand, typeof(int));
BinaryExpression BrandExpr = Expression.Equal(BrandPropExpr, BrandValueExpr);
if (i == ) {
whereBrandExpr = BrandExpr;
}
else { whereBrandExpr = Expression.Or(whereBrandExpr, BrandExpr); } }
} Expression wherecategoryExpr = null;
if (categorys != null && categorys.Length > ) {
for (int i = , j = categorys.Length; i < j; i++) {
int category = categorys[i];
MemberExpression categoryPropExpr = Expression.Property(paramExpr, "Category");
ConstantExpression categoryValueExpr = Expression.Constant(category, typeof(int));
BinaryExpression categoryExpr = Expression.Equal(categoryPropExpr, categoryValueExpr);
if (wherecategoryExpr == null) {
if (i == ) {
wherecategoryExpr = categoryExpr;
}//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
else {
wherecategoryExpr = Expression.Or(wherecategoryExpr, categoryExpr);
}
}
else {
wherecategoryExpr = Expression.Or(wherecategoryExpr, categoryExpr);
}
}
} if (whereBrandExpr != null) {
whereExpr = Expression.And(whereExpr, whereBrandExpr);
}
if (wherecategoryExpr != null) {
whereExpr = Expression.And(whereExpr, wherecategoryExpr);
} Expression<Func<Product, bool>> lambda = Expression.Lambda<Func<Product, bool>>(whereExpr, paramExpr); switch (orderBy) {
case "priceasc":
product = BLLRequest.Current.ProductCollection.Where(lambda.Compile()).OrderBy(it => it.UnitPrice);
break;
case "pricedesc":
product = BLLRequest.Current.ProductCollection.Where(lambda.Compile()).OrderByDescending(it => it.UnitPrice);
break;
case "salesasc":
product = BLLRequest.Current.ProductCollection.Where(lambda.Compile()).OrderBy(it => it.SalesQty);
break;
case "salesdesc":
product = BLLRequest.Current.ProductCollection.Where(lambda.Compile()).OrderByDescending(it => it.SalesQty);
break;
default:
product = BLLRequest.Current.ProductCollection.Where(lambda.Compile()).OrderBy(it => it.UnitPrice);
break;
} int total = product.Count();
int pagecount = (total % pagesize) > ? (total / pagesize) + : (total / pagesize);
product = product.Skip((pageindex - ) * pagesize).Take(pagesize);
ViewBag.product = product;
ViewBag.total = total;
ViewBag.pagecount = pagecount;
ViewBag.pageindex = pageindex;
return PartialView();
}
ahjesus动态生成表达式树的更多相关文章
- 泛型方法动态生成表达式树 Expression
public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...
- 表达式树扩展 动态生成表达式树插件 Sy.ExpressionBuilder。
CURD中,基础查询我感觉还是很烦人的一个浪费时间的工作,我经历过远古时代的GetAll(string name,int age),这种方式写服务的时候真的是心中一万个草泥马飞过,后面逐渐的变成了传一 ...
- c# 表达式目录树拷贝对象(根据对象类型动态生成表达式目录树)
表达式目录树,在C#中用Expression标识,这里就不介绍表达式目录树是什么了,有兴趣可以自行百度搜索,网上资料还是很多的. 这里主要分享的是如何动态构建表达式目录树. 构建表达式目录树的代码挺简 ...
- C# 动态构建表达式树(一)—— 构建 Where 的 Lambda 表达式
C# 动态构建表达式树(一)-- 构建 Where 的 Lambda 表达式 前言 记得之前同事在做筛选功能的时候提出过一个问题:如果用户传入的条件数量不确定,条件的内容也不确定(大于.小于和等于), ...
- LinqToDB 源码分析——生成表达式树
当我们知道了Linq查询要用到的数据库信息之后.接下就是生成对应的表达式树.在前面的章节里面笔者就已经介绍过.生成表达式树是事实离不开IQueryable<T>接口.而处理表达式树离不开I ...
- Lind.DDD.ExpressionExtensions动态构建表达式树,实现对数据集的权限控制
回到目录 Lind.DDD框架里提出了对数据集的控制,某些权限的用户为某些表添加某些数据集的权限,具体实现是在一张表中存储用户ID,表名,检索字段,检索值和检索操作符,然后用户登陆后,通过自己权限来构 ...
- C#动态构建表达式树(三)——表达式的组合
C#动态构建表达式树(三)--表达式的组合 前言 在筛选数据的过程中,可能会有这样的情况:有一些查询条件是公共的,但是根据具体的传入参数可能需要再额外增加一个条件.对于这种问题一般有两种方法: a. ...
- C# 动态构建表达式树(二)——构建 Select 和 GroupBy 的表达式
C# 动态构建表达式树(二)--构建 Select 和 GroupBy 的表达式 前言 在上篇中写了表达式的基本使用,为 Where 方法动态构建了表达式.在这篇中会写如何为 Select 和 Gro ...
- js动态生成JSON树
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- 入手Cubieboard2之制作最小Linux系统
前言 昨天终于入手了一块Cubieboard2板子,今年4月入职从事的就是与之相关的工作,因此趁现在有时间就好好熟悉一下. 一.主机环境 1.PC主机WIN 7旗舰版 1.虚拟机VM7.0 2.ubu ...
- 为Eclipse添加Java和Android SDK源代码
1.添加jdk源码进入eclipse Ctrl + Click -->Attached Source 路径:D:\Program Files\Java\jdk1.8.0_45\src.zip 2 ...
- 我的首个MOOC课程《面向对象软件开发实践》
我的首个MOOC课程<面向对象软件开发实践> 我将在网易云课堂开讲MOOC课<面向对象软件开发实践>(http://mooc.study.163.com/course/YOOK ...
- C# 调用 WebService 连接ORACLE 11g
这几天开发一个WebService遇到很多问题,记录下来顺便帮助一下以后遇到情况的人. 我是通过ADO.NET来连接ORACLE的,也可以用ORACLE提供的ODP.NET. 通过正常的连接后部署II ...
- Cannot open connection 解决办法
试了很多种网上找的办法,都不行,最后才发现是我的beans.xml中完全把下面 这一段代码给遗忘了,忘记写了.添加我就ok了. 我能说花了我近1个小时吗?坑姐哦! <bean class=&qu ...
- 纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦
http://css-tricks.com/examples/ShapesOfCSS/
- Knockout 新版应用开发教程之Computed Observables
Computed Observables 如果你有监控属性firstName和lastName的话,此时如果你想要显示全名? 这个时候computed(以前叫做依赖)监控属性就出马了,这是一个函数用来 ...
- Java--解决java compiler level does not match the version of the installed java project facet错误
从网上下载了个项目,导入eclipse时发现如下错误: 检查后发现原来是JDK版本不一致的问题,项目的默认JDK配置是1.5,而自己电脑上是1.7. 需要修改两个地方的配置,如下图所示: 修改一: 修 ...
- Clipping Magic – 帮助你轻松删除图片背景
Clipping Magic 让您轻松去除图片的背景,可以根据路径进行裁剪.操作很简单,只需拖动图像到放置区,或使用按钮选择文件.标记前景为绿,背景为红色,然后标记算法会帮助你处理好细节.处理后的图片 ...
- 关于开发Windows服务程序容易搞混的地方!
在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...