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

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. IE11兼容性设定

    增加browser文件,如下:  <browsers>   <browser refID="Default">     <capabilities&g ...

  2. Domain Space

    Bluehost Register Page http://www.bluehost.com/track/weipengp

  3. 免费国内外"代码托管服务器"收集

      国内 开源中国  http://git.oschina.net/  支持git 淘宝code  http://code.taobao.org/  支持svn 京东code  https://cod ...

  4. [原]SQLite的学习系列之获取数据库版本二

    本系列文章主要是使用C++语言来调用其API,达到管中窥豹的目的.另外本文使用的开发环境为mac + clion,并且基于SQLite 3.7.14来进行开发. 一.去下载sqlite-amalgam ...

  5. php生成随机密码(php自定义函数)转自先锋教程网

    php生成随机密码(php自定义函数) 时间:2015-12-16 20:43:49来源:网络 导读:php随机密码的生成代码,使用php自定义函数生成指定长度的随机密码,密码规则为小写字母与数字的随 ...

  6. jquery select选中表单特效三级联动

    好久没发帖了,今天发一个jquery select的表单三级联动,希望能对有需要的朋友有点帮助. js代码,当然还要加上jquery的包,应该都有的,这里就不发了. <script type=& ...

  7. NavMesh系统动态碰撞的探讨

    Unity3D提供的NavMesh系统可以方便的解决游戏的寻路问题,但是该系统有一个比较让人不理解的问题: NavMesh导航时会忽略Physics系统本身的碰撞,也就是说NavMeshAgent在移 ...

  8. Unity 模型导入导出

    从3DMAX导出,参考: http://tieba.baidu.com/p/2807225555 -> 使用3dmax 2013,会自带导出 fbx 的功能 -> 从 3dmax 导出 - ...

  9. 剑指架构师系列-Struts2构造函数的循环依赖注入

    Struts2可以完成构造函数的循环依赖注入,来看看Struts2的大师们是怎么做到的吧! 首先定义IBlood与BloodImpl类: public interface IBlood { } pub ...

  10. 探索jdk8之ConcurrentHashMap 的实现机制

    在介绍ConcurrentHashMap源码之前,很有必要复习下java并发编程中的一些基础知识,比如内存模型等. 存储模型 并发编程中的三个概念 1.原子性 2.可见性 3.重排序 对HashMap ...