Пример #1
0
        public ActionResult Index(string k, string c = "", int page = 1, int pageSize = 15)
        {
            int count;

            count = 0;
            //使用缓存搜索
            var query = new CRL.ExpressionJoin <Product>(BLL.ProductManage.Instance.AllCache, b => b.ProductStatus == CRL.Package.Product.ProductStatus.已上架);

            if (!string.IsNullOrEmpty(k))
            {
                query.And(b => b.ProductName.Contains(k));
            }
            if (!string.IsNullOrEmpty(c))
            {
                query.And(b => b.CategoryCode.StartsWith(c));
            }

            var products = query.ToList().OrderByDescending(b => b.Id);

            count = products.Count();
            var result  = products.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            var pageObj = new PageObj <Product>(result, page, count, pageSize);

            return(View(pageObj));
        }
Пример #2
0
        protected void Button3_Click(object sender, EventArgs e)
        {
            var list = Code.ProductDataManage.Instance.AllCache;//指定一个数据源

            #region 常规查找 多次计算和内存操作,增加成本
            var  list2 = list.Where(b => b.Id > 0);//执行一次内存查找
            bool a     = false;
            if (a)
            {
                list2 = list.Where(b => b.Number > 10);//执行第二次内存查找
            }
            #endregion

            #region 优化后查找 只需一次
            var query = new CRL.ExpressionJoin <Code.ProductData>(list, b => b.Id > 0);
            if (a)
            {
                query.And(b => b.Number > 10); //and 一个查询条件
            }
            list2 = query.ToList();            //返回查询结果 只作一次内存查找
            #endregion
        }