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

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动态生成表达式树的更多相关文章

  1. 泛型方法动态生成表达式树 Expression

    public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...

  2. 表达式树扩展 动态生成表达式树插件 Sy.ExpressionBuilder。

    CURD中,基础查询我感觉还是很烦人的一个浪费时间的工作,我经历过远古时代的GetAll(string name,int age),这种方式写服务的时候真的是心中一万个草泥马飞过,后面逐渐的变成了传一 ...

  3. c# 表达式目录树拷贝对象(根据对象类型动态生成表达式目录树)

    表达式目录树,在C#中用Expression标识,这里就不介绍表达式目录树是什么了,有兴趣可以自行百度搜索,网上资料还是很多的. 这里主要分享的是如何动态构建表达式目录树. 构建表达式目录树的代码挺简 ...

  4. C# 动态构建表达式树(一)—— 构建 Where 的 Lambda 表达式

    C# 动态构建表达式树(一)-- 构建 Where 的 Lambda 表达式 前言 记得之前同事在做筛选功能的时候提出过一个问题:如果用户传入的条件数量不确定,条件的内容也不确定(大于.小于和等于), ...

  5. LinqToDB 源码分析——生成表达式树

    当我们知道了Linq查询要用到的数据库信息之后.接下就是生成对应的表达式树.在前面的章节里面笔者就已经介绍过.生成表达式树是事实离不开IQueryable<T>接口.而处理表达式树离不开I ...

  6. Lind.DDD.ExpressionExtensions动态构建表达式树,实现对数据集的权限控制

    回到目录 Lind.DDD框架里提出了对数据集的控制,某些权限的用户为某些表添加某些数据集的权限,具体实现是在一张表中存储用户ID,表名,检索字段,检索值和检索操作符,然后用户登陆后,通过自己权限来构 ...

  7. C#动态构建表达式树(三)——表达式的组合

    C#动态构建表达式树(三)--表达式的组合 前言 在筛选数据的过程中,可能会有这样的情况:有一些查询条件是公共的,但是根据具体的传入参数可能需要再额外增加一个条件.对于这种问题一般有两种方法: a. ...

  8. C# 动态构建表达式树(二)——构建 Select 和 GroupBy 的表达式

    C# 动态构建表达式树(二)--构建 Select 和 GroupBy 的表达式 前言 在上篇中写了表达式的基本使用,为 Where 方法动态构建了表达式.在这篇中会写如何为 Select 和 Gro ...

  9. js动态生成JSON树

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. WeifenLuo组件中如何设置停靠窗体的宽度

    在项目中使用了WeifenLuo.WinFormsUI.Docking组件,窗体停靠效果非常棒. 现在项目出现了这样的需求,希望可以控制停靠窗体的宽度,因为默认的宽度往往会造成停靠窗体的内容显示不完全 ...

  2. 操作系统性能分析与优化V1.0

    操作系统性能分析与优化V1.0 : http://www.docin.com/p-759561760.html

  3. js/jquery 实时监听输入框值变化的完美方案:oninput & onpropertychange

    (1)     先说jquery, 使用 jQuery 库的话,只需要同时绑定 oninput 和 onpropertychange 两个事件就可以了,示例代码: $('#username').bin ...

  4. hadoop的dfs工具类一个【原创】

    开始没搞定插件问题,就弄了个dsf操作类,后面搞定了插件问题,这玩意也就聊胜于无了,还是丢这里算了. 首先是一个配置,ztool.hadoop.properties hadoop.home.dir=G ...

  5. 6x12了快2个月,累的不行……

    6x12了快2个月,累的不行…… 咱就是传说中的会iOS, Android, .NET,JAVA, JAVASCRIPT,SQL SERVER的 Full stack developer (全端工程师 ...

  6. 使用Javascript来创建一个响应式的超酷360度全景图片查看幻灯效果

    360度的全景图片效果常常可以用到给客户做产品展示,今天这里我们推荐一个非常不错的来自Robert Pataki的360全景幻灯实现教程,这里教程中将使用javascript来打造一个超酷的全景幻灯实 ...

  7. Android Material Design控件学习(一)——TabLayout的用法

    前言 Google官方在14年Google I/O上推出了全新的设计语言--Material Design.一并推出了一系列实现Material Design效果的控件库--Android Desig ...

  8. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. access里like的通配符不能用%,要用*

    转自http://www.knowsky.com/339881.html access里like的通配符用法是这样:     “?”表示任何单一字符: “*”表示零个或多个字符: “#”表示任何一个数 ...

  10. [Git] Git 常用技巧

    版本对比 1. 对比两个 COMMIT git diff <commit> <commit> 2. 对比 COMMIT 和父 COMMIT git diff <commi ...