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

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. web app变革之rem(手机屏幕实现全适配)

    以往web移动适配,常规写法是:media only screen @media only screen and (min-device-width: 320px){ //针对iPhone 3 } @ ...

  2. C# 文件操作方法

    方法一: FileStream textFile = File.Open(@"F:\程序\新手测试\linqApplication1\linqApplication1\IO.txt" ...

  3. 用Wireshark简单分析HTTP通信

    我们都学过TCP,HTTP的相关概念,本文借助协议分析工具Wireshark,让大家对一些概念眼见为实,权当温故而知新. 场景: 在Client(10.239.196.211)上通过web brows ...

  4. Vim安装jedi-vim提示的一个错误

    (仅为了提醒自己) 第一次的安装方法好像是通过 bundle安装的,好像是通过这个安装的并不是最新的版本,然后删除了通过下面的方法,最重要的是要执行 git submodule update --in ...

  5. PostgreSQL表空间、模式、表、用户/角色之间的关系

    PostgreSQL表空间.模式.表.用户/角色之间的关系是本文我们主要要介绍的内容,表空间,数据库,模式,表,用户,角色之间的关系到底是怎样的呢?接下来我们就开始介绍这一过程. 实验出角色与用户的关 ...

  6. 使用UIKit制作卡牌游戏(二)ios游戏篇

    转自朋友Tommy 的翻译,自己只翻译了第三篇教程. 译者: Tommy | 原文作者: Matthijs Hollemans写于2012/07/06 原文地址: http://www.raywend ...

  7. 二十六、【开源框架】EFW框架Winform前端开发之Grid++Report报表、条形码、Excel导出、图表控件

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...

  8. 如何复制DataRow(dataTabel中的行)

    由于需要对dataTabel中的行进行上移和下移操作: row 1      行号0 row2       行号1 row3       行号2 例如将row3上移一行,即row2和row3对调位置. ...

  9. Netty5 + WebSocket 练习

    1. 了解WebSocket知识 略2. websocket实现系统简单反馈时间 WebSocketServerHandler.java package com.jieli.nettytest.web ...

  10. excel导入记录

    use DangJianSELECT vale1, value2 into Table2 from Table1 select COUNT(*) from tmpdangyuan where 手机号 ...