//根據搜尋來取得資料陣列的方法 public List <Products> GetDataList(ForPaging Paging, ProductsView searchCondition) { //宣告要接受全部搜尋資料的物件 IQueryable <Products> SearchData; SearchData = db.Products; string productID = ""; string productName = ""; if (searchCondition.ProductID != null) { productID = searchCondition.ProductID.ToString().Trim(); } if (searchCondition.ProductName != null) { productName = searchCondition.ProductName.Trim(); } if (String.IsNullOrEmpty(productID) == false) { SearchData = SearchData.Where(p => p.ProductID.ToString().Contains(productID)); } if (String.IsNullOrEmpty(productName) == false) { SearchData = SearchData.Where(p => p.ProductName.ToString().Contains(productName)); } //計算所需的總頁數 Paging.MaxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(SearchData.Count()) / Paging.ItemNum)); //重新設定正確的頁數,避免有不正確值傳入 Paging.SetRightPage(); //先排序再根據分頁來回傳所需部分的資料陣列 return(SearchData.OrderByDescending(p => p.ProductID).Skip((Paging.NowPage - 1) * Paging.ItemNum).Take(Paging.ItemNum).ToList()); }