public ListProductModel Query(FilterModel filter) { int offset = (filter.page - 1) * filter.view; int count = filter.view; IQueryable<SanPham> query = null; if (filter.query != null && filter.query.Trim() != "") { string name = filter.query; query = SearchByName(name, offset, count); } else { string sql = CreatSqlQuery(filter); query = (((IObjectContextAdapter)db).ObjectContext.CreateQuery<SanPham>(sql)); } ListProductModel result = new ListProductModel(); string[] param = filter.sortby.Split('_'); bool order_price = param[0] == "price" ? true : false; bool asc = param[1] == "asc" ? true : false; if (order_price) { if (asc) { result.data = query.OrderBy(x => x.GiaBanHienHanh).Skip(offset).Take(count).ToList(); } else { result.data = query.OrderByDescending(x => x.GiaBanHienHanh).Skip(offset).Take(count).ToList(); } } else { if (asc) { result.data = query.OrderBy(x => x.TenSP).Skip(offset).Take(count).ToList(); } else { result.data = query.OrderByDescending(x => x.TenSP).Skip(offset).Take(count).ToList(); } } foreach (SanPham sp in result.data) { sp.ChiTietSanPham = GetDetailProductWithoutImage(sp.MaCTSP); } result.count = query.Count(); return result; }
private IQueryable<SanPham> SearchByName(string name, int offset, int count) { ListProductModel result = new ListProductModel(); var query = from sp in db.SanPhams where sp.TenSP.ToLower().Contains(name.ToLower()) select sp; return query; }