ahjesus动态生成表达式树

时间:2022-06-24 18:46:32

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

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<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 > 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<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) > 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();
        }