博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ahjesus动态生成表达式树
阅读量:6886 次
发布时间:2019-06-27

本文共 4921 字,大约阅读时间需要 16 分钟。

直接上方法,看的懂的拿去用,看不懂的找资料看懂

public PartialViewResult _Product(int pageindex = 1, int pagesize = 12, Double floorprice = 0, Double topprice = 9999999999, string brandstr = "", string categorystr = "", string orderBy = "priceasc") {            int[] brands;            if (string.IsNullOrWhiteSpace(brandstr)) {                brands = null;            }            else {                brands = Array.ConvertAll
(brandstr.Split(','), delegate(string s) { return int.Parse(s); }); } int[] categorys; if (string.IsNullOrWhiteSpace(categorystr)) { categorys = null; } else { categorys = Array.ConvertAll
(categorystr.Split(','), delegate(string s) { return int.Parse(s); }); } IEnumerable
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 > 0) { for (int i = 0, 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 == 0) { whereBrandExpr = BrandExpr; } else { whereBrandExpr = Expression.Or(whereBrandExpr, BrandExpr); } } } Expression wherecategoryExpr = null; if (categorys != null && categorys.Length > 0) { for (int i = 0, 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 == 0) { 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
> lambda = Expression.Lambda
>(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) > 0 ? (total / pagesize) + 1 : (total / pagesize); product = product.Skip((pageindex - 1) * pagesize).Take(pagesize); ViewBag.product = product; ViewBag.total = total; ViewBag.pagecount = pagecount; ViewBag.pageindex = pageindex; return PartialView(); }

 

 

 

转载于:https://www.cnblogs.com/ahjesus/p/3402361.html

你可能感兴趣的文章
Tarjin + 缩点
查看>>
Sqli-labs less 35
查看>>
python中字典的用法
查看>>
【Spark 深入学习 07】RDD编程之旅基础篇03-键值对RDD
查看>>
汽车常识全面介绍 - 引擎概论
查看>>
教你如何拍好人像摄影
查看>>
python 闭包
查看>>
学习笔记-备份还原
查看>>
android WebView缩放时卡顿问题
查看>>
FastDFS搭建及java整合代码【转】
查看>>
nginx gzip压缩
查看>>
不要在该约炮的年纪谈佛系
查看>>
c++SDK c#调用_疑难杂症
查看>>
Git初始化项目 和 Gitignore
查看>>
CMakeList.txt设置OpenCv路径
查看>>
springboot mvc beetl模板 自定义错误的后缀问题
查看>>
ext常用属性
查看>>
PL/SQL连接64位Oracle配置方法
查看>>
socket解读,http和socket之长连接和短连接区别!
查看>>
洛谷——P1165 日志分析
查看>>