直接上方法,看的懂的拿去用,看不懂的找资料看懂
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();
}