完成 Batch 2:修復 4 個 API Controller 的 AsEnumerable 問題
修復文件(共 21 處): 1. newsController.cs (7 處) - 移除簡單查詢的 AsEnumerable() - AsEnumerable() → AsQueryable() (GetList, GetKindList) - 優化分頁查詢模式(Count + ToPagedList.ToList()) - 修復 Split().Select().ToList() 類型問題 2. FilesSetController.cs (5 處) - 移除簡單查詢的 AsEnumerable() - AsEnumerable() → AsQueryable() (GetList) - 優化分頁查詢模式 - 修復 files_num nullable 字段處理 - 修復 Split().Select().ToList() 類型問題 3. bedController.cs (5 處) - 移除簡單查詢的 AsEnumerable() - AsEnumerable() → AsQueryable() (companies, bed_order_detail) - 優化分頁查詢模式 - 簡化查詢條件 4. bed_kindController.cs (4 處) - AsEnumerable() → AsQueryable() (bed_kind, bed_kind_detail) - 優化分頁查詢模式(兩處) - 優化 bedDt 查詢:移除 AsEnumerable(),改用 Where + ToList() - 修復 bed_kind_detail_id nullable 字段處理 技術改進: ✓ 所有查詢改為在數據庫執行 ✓ 統一使用三元運算符處理分頁 ✓ Count 在分頁前執行 ✓ 正確處理 nullable 字段(HasValue + .Value) ✓ IEnumerable<int> → List<int> 避免類型轉換錯誤
This commit is contained in:
@@ -52,7 +52,7 @@ public class FilesSetController : ApiController
|
|||||||
// DELETE api/<controller>/5
|
// DELETE api/<controller>/5
|
||||||
public void Delete(int id)
|
public void Delete(int id)
|
||||||
{
|
{
|
||||||
var prod = _db.files.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
var prod = _db.files.Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||||||
if (prod != null)
|
if (prod != null)
|
||||||
{
|
{
|
||||||
//var prod2 = _db.actItem_files.AsEnumerable().Where(q => q.files_num == id).ToList(); //刪除品項的相關文件
|
//var prod2 = _db.actItem_files.AsEnumerable().Where(q => q.files_num == id).ToList(); //刪除品項的相關文件
|
||||||
@@ -84,13 +84,13 @@ public class FilesSetController : ApiController
|
|||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(nums))
|
if (!string.IsNullOrEmpty(nums))
|
||||||
{
|
{
|
||||||
var ids = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s));
|
var ids = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s)).ToList();
|
||||||
|
|
||||||
var prod = _db.files.AsEnumerable().Where(q => ids.Contains(q.num)).ToList();
|
var prod = _db.files.Where(q => ids.Contains(q.num)).ToList();
|
||||||
if (prod.Count() > 0)
|
if (prod.Count() > 0)
|
||||||
{
|
{
|
||||||
//刪除品項的相關文件
|
//刪除品項的相關文件
|
||||||
var prod2 = _db.actItem_files.AsEnumerable().Where(q => ids.Contains(Convert.ToInt32(q.files_num))).ToList();
|
var prod2 = _db.actItem_files.Where(q => q.files_num.HasValue && ids.Contains(q.files_num.Value)).ToList();
|
||||||
if (prod2.Count > 0)
|
if (prod2.Count > 0)
|
||||||
{
|
{
|
||||||
_db.actItem_files.RemoveRange(prod2);
|
_db.actItem_files.RemoveRange(prod2);
|
||||||
@@ -123,7 +123,7 @@ public class FilesSetController : ApiController
|
|||||||
public IHttpActionResult GetList([FromBody] Model.ViewModel.files q, int page, int pageSize = 10,
|
public IHttpActionResult GetList([FromBody] Model.ViewModel.files q, int page, int pageSize = 10,
|
||||||
string sortBy = "", bool sortDesc = false)
|
string sortBy = "", bool sortDesc = false)
|
||||||
{
|
{
|
||||||
var qry = _db.files.AsEnumerable();
|
var qry = _db.files.AsQueryable();
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(q.subject))
|
if (!string.IsNullOrEmpty(q.subject))
|
||||||
qry = qry.Where(o => o.subject.Contains(q.subject));
|
qry = qry.Where(o => o.subject.Contains(q.subject));
|
||||||
@@ -157,9 +157,12 @@ public class FilesSetController : ApiController
|
|||||||
else
|
else
|
||||||
qry = qry.OrderByDescending(o => o.num);
|
qry = qry.OrderByDescending(o => o.num);
|
||||||
|
|
||||||
|
var count = qry.Count();
|
||||||
|
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
||||||
|
|
||||||
var ret = new
|
var ret = new
|
||||||
{
|
{
|
||||||
list = qry.ToPagedList(page, pageSize).Select(x => new
|
list = qryList.Select(x => new
|
||||||
{
|
{
|
||||||
num = x.num,
|
num = x.num,
|
||||||
subject = x.subject,
|
subject = x.subject,
|
||||||
@@ -168,7 +171,7 @@ public class FilesSetController : ApiController
|
|||||||
word = x.word,
|
word = x.word,
|
||||||
|
|
||||||
}),
|
}),
|
||||||
count = qry.Count()
|
count = count
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public class bedController : BaseApiController
|
|||||||
// DELETE api/<controller>/5
|
// DELETE api/<controller>/5
|
||||||
public void Delete(string id)
|
public void Delete(string id)
|
||||||
{
|
{
|
||||||
var prod = _db.bed_order.AsEnumerable().Where(q => q.bed_order_no == id).FirstOrDefault(); //刪除該筆資料
|
var prod = _db.bed_order.Where(q => q.bed_order_no == id).FirstOrDefault(); //刪除該筆資料
|
||||||
if (prod != null)
|
if (prod != null)
|
||||||
{
|
{
|
||||||
//刪除訂單明細
|
//刪除訂單明細
|
||||||
@@ -80,7 +80,7 @@ public class bedController : BaseApiController
|
|||||||
{
|
{
|
||||||
order_no = "ED" + DateTime.Now.ToString("yyMMdd");
|
order_no = "ED" + DateTime.Now.ToString("yyMMdd");
|
||||||
|
|
||||||
var qry = _db.companies.AsEnumerable();
|
var qry = _db.companies.AsQueryable();
|
||||||
var prod = qry.Where(q => q.num == 1).FirstOrDefault();
|
var prod = qry.Where(q => q.num == 1).FirstOrDefault();
|
||||||
if (prod != null)
|
if (prod != null)
|
||||||
{
|
{
|
||||||
@@ -157,16 +157,18 @@ public class bedController : BaseApiController
|
|||||||
public IHttpActionResult GetDetailList([FromBody] Model.bed_order q,
|
public IHttpActionResult GetDetailList([FromBody] Model.bed_order q,
|
||||||
int page, int pageSize = 10, string sortBy = "", bool sortDesc = false)
|
int page, int pageSize = 10, string sortBy = "", bool sortDesc = false)
|
||||||
{
|
{
|
||||||
var qry = _db.bed_order_detail.AsEnumerable();
|
var qry = _db.bed_order_detail.Where(o => o.bed_order_no == q.bed_order_no);
|
||||||
qry = qry.Where(o => o.bed_order_no == q.bed_order_no);
|
|
||||||
|
|
||||||
qry = qry.OrderBy(o => o.checkIn_date ).ThenBy(o => o.num);
|
qry = qry.OrderBy(o => o.checkIn_date ).ThenBy(o => o.num);
|
||||||
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
var tdesc = publicFun.enum_desc<Model.bed_kind.bed_type>();
|
var tdesc = publicFun.enum_desc<Model.bed_kind.bed_type>();
|
||||||
|
var count = qry.Count();
|
||||||
|
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
||||||
|
|
||||||
var ret = new
|
var ret = new
|
||||||
{
|
{
|
||||||
list = qry.ToPagedList(page, pageSize).Select(x => new
|
list = qryList.Select(x => new
|
||||||
{
|
{
|
||||||
id = i++,
|
id = i++,
|
||||||
num = x.num,
|
num = x.num,
|
||||||
@@ -197,7 +199,7 @@ public class bedController : BaseApiController
|
|||||||
},
|
},
|
||||||
license = x.license,
|
license = x.license,
|
||||||
}),
|
}),
|
||||||
count = qry.Count()
|
count = count
|
||||||
|
|
||||||
};
|
};
|
||||||
return Ok(ret);
|
return Ok(ret);
|
||||||
@@ -250,7 +252,7 @@ public class bedController : BaseApiController
|
|||||||
[Route("api/bed/DeleteBedDetail/{id}")]//刪除detail
|
[Route("api/bed/DeleteBedDetail/{id}")]//刪除detail
|
||||||
public void DeleteBedDetail(int id)
|
public void DeleteBedDetail(int id)
|
||||||
{
|
{
|
||||||
var prod = _db.bed_order_detail.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
var prod = _db.bed_order_detail.Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||||||
if (prod != null)
|
if (prod != null)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class bed_kindController : ApiController
|
|||||||
public IHttpActionResult GetList([FromBody] Model.ViewModel.bed_kind q,
|
public IHttpActionResult GetList([FromBody] Model.ViewModel.bed_kind q,
|
||||||
int page, int pageSize = 10, string sortBy = "", bool sortDesc = false)
|
int page, int pageSize = 10, string sortBy = "", bool sortDesc = false)
|
||||||
{
|
{
|
||||||
var qry = _db.bed_kind.AsEnumerable();
|
var qry = _db.bed_kind.AsQueryable();
|
||||||
if (!string.IsNullOrEmpty(q.kind))
|
if (!string.IsNullOrEmpty(q.kind))
|
||||||
qry = qry.Where(o => o.kind.Contains(q.kind));
|
qry = qry.Where(o => o.kind.Contains(q.kind));
|
||||||
if (!string.IsNullOrEmpty(q.sex))
|
if (!string.IsNullOrEmpty(q.sex))
|
||||||
@@ -60,16 +60,20 @@ public class bed_kindController : ApiController
|
|||||||
if (q.root.HasValue)
|
if (q.root.HasValue)
|
||||||
qry = qry.Where(o => o.root.Value == q.root.Value);
|
qry = qry.Where(o => o.root.Value == q.root.Value);
|
||||||
qry = qry.OrderBy(o => o.range).ThenBy(o => o.num);
|
qry = qry.OrderBy(o => o.range).ThenBy(o => o.num);
|
||||||
|
|
||||||
|
var count = qry.Count();
|
||||||
|
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
||||||
|
|
||||||
var ret = new
|
var ret = new
|
||||||
{
|
{
|
||||||
list = qry.ToPagedList(page, pageSize).Select(x => new
|
list = qryList.Select(x => new
|
||||||
{
|
{
|
||||||
num = x.num,
|
num = x.num,
|
||||||
kind = x.kind,
|
kind = x.kind,
|
||||||
sex = x.sex,
|
sex = x.sex,
|
||||||
|
|
||||||
}),
|
}),
|
||||||
count = qry.Count()
|
count = count
|
||||||
|
|
||||||
};
|
};
|
||||||
return Ok(ret);
|
return Ok(ret);
|
||||||
@@ -83,7 +87,7 @@ public class bed_kindController : ApiController
|
|||||||
var tdesc = publicFun.enum_desc<Model.bed_kind.bed_type>();
|
var tdesc = publicFun.enum_desc<Model.bed_kind.bed_type>();
|
||||||
//var tdesc2 = tdesc.ToArray().Select(x=>x.Value); //[0]單人床,[1]雙人床
|
//var tdesc2 = tdesc.ToArray().Select(x=>x.Value); //[0]單人床,[1]雙人床
|
||||||
//var tdesc3 = tdesc.ToArray().Select(x=>x.Key); //[0]1,[1]2
|
//var tdesc3 = tdesc.ToArray().Select(x=>x.Key); //[0]1,[1]2
|
||||||
var qry = _db.bed_kind_detail.AsEnumerable();
|
var qry = _db.bed_kind_detail.AsQueryable();
|
||||||
if (q.bed_kind_id.HasValue)
|
if (q.bed_kind_id.HasValue)
|
||||||
qry = qry.Where(o => o.bed_kind_id == q.bed_kind_id);
|
qry = qry.Where(o => o.bed_kind_id == q.bed_kind_id);
|
||||||
if (!string.IsNullOrEmpty(q.bed_name))
|
if (!string.IsNullOrEmpty(q.bed_name))
|
||||||
@@ -103,17 +107,19 @@ public class bed_kindController : ApiController
|
|||||||
if (q.inTime.HasValue )
|
if (q.inTime.HasValue )
|
||||||
{
|
{
|
||||||
//判斷日期沒庫存不能選
|
//判斷日期沒庫存不能選
|
||||||
var bedDt = _db.bed_order_detail.AsEnumerable().Where(f => f.checkIn_date.HasValue && f.checkIn_date ==q.inTime.Value ).Select(f => f.bed_kind_detail_id.ToString());//掛單表單明細
|
var bedDt = _db.bed_order_detail.Where(f => f.checkIn_date.HasValue && f.checkIn_date ==q.inTime.Value && f.bed_kind_detail_id.HasValue).Select(f => f.bed_kind_detail_id.Value).ToList();//掛單表單明細
|
||||||
qry = qry.Where(o => !bedDt.ToArray().Contains(o.num.ToString()));
|
qry = qry.Where(o => !bedDt.Contains(o.num));
|
||||||
|
|
||||||
}
|
}
|
||||||
qry = qry.OrderBy(o => o.bed_name);
|
qry = qry.OrderBy(o => o.bed_name);
|
||||||
int i = 1;
|
int i = 1;
|
||||||
|
|
||||||
if (pageSize > 0) qry = qry.ToPagedList(page, pageSize);
|
var count = qry.Count();
|
||||||
|
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
||||||
|
|
||||||
var ret = new
|
var ret = new
|
||||||
{
|
{
|
||||||
list = qry.Select(x => new
|
list = qryList.Select(x => new
|
||||||
{
|
{
|
||||||
id = i++,
|
id = i++,
|
||||||
num = x.num,
|
num = x.num,
|
||||||
@@ -122,7 +128,7 @@ public class bed_kindController : ApiController
|
|||||||
bed_type_txt = x.bed_type.HasValue? tdesc[x.bed_type ?? 1] : "",
|
bed_type_txt = x.bed_type.HasValue? tdesc[x.bed_type ?? 1] : "",
|
||||||
demo = x.demo,
|
demo = x.demo,
|
||||||
}),
|
}),
|
||||||
count = qry.Count()
|
count = count
|
||||||
|
|
||||||
};
|
};
|
||||||
return Ok(ret);
|
return Ok(ret);
|
||||||
@@ -186,7 +192,7 @@ public class bed_kindController : ApiController
|
|||||||
[Route("api/bed_kind/DeleteBedKindDetail/{id}")]//刪除分類的detail
|
[Route("api/bed_kind/DeleteBedKindDetail/{id}")]//刪除分類的detail
|
||||||
public void DeleteActKindDetail(int id)
|
public void DeleteActKindDetail(int id)
|
||||||
{
|
{
|
||||||
var prod = _db.bed_kind_detail.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
var prod = _db.bed_kind_detail.Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||||||
if (prod != null)
|
if (prod != null)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -49,11 +49,11 @@ public class newsController : BaseApiController
|
|||||||
// DELETE api/<controller>/5
|
// DELETE api/<controller>/5
|
||||||
public void Delete(int id)
|
public void Delete(int id)
|
||||||
{
|
{
|
||||||
var prod = _db.news.AsEnumerable().Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
var prod = _db.news.Where(q => q.num == id).FirstOrDefault(); //刪除該筆資料
|
||||||
if (prod != null)
|
if (prod != null)
|
||||||
{
|
{
|
||||||
//刪除檔案
|
//刪除檔案
|
||||||
var prod2 = _db.news_files.AsEnumerable().Where(q => q.news_id == id).ToList();
|
var prod2 = _db.news_files.Where(q => q.news_id == id).ToList();
|
||||||
if (prod2.Count > 0)
|
if (prod2.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var item in prod2)
|
foreach (var item in prod2)
|
||||||
@@ -91,12 +91,12 @@ public class newsController : BaseApiController
|
|||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(nums))
|
if (!string.IsNullOrEmpty(nums))
|
||||||
{
|
{
|
||||||
var getDelItem = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s));
|
var getDelItem = nums.TrimEnd(',').Split(',').Select(s => int.Parse(s)).ToList();
|
||||||
|
|
||||||
var prod = _db.news.AsEnumerable().Where(q => getDelItem.Contains(q.num)).ToList();
|
var prod = _db.news.Where(q => getDelItem.Contains(q.num)).ToList();
|
||||||
if (prod.Count() > 0)
|
if (prod.Count() > 0)
|
||||||
{
|
{
|
||||||
var prod2 = _db.news_files.AsEnumerable().Where(q => getDelItem.Contains(q.news_id)).ToList();
|
var prod2 = _db.news_files.Where(q => getDelItem.Contains(q.news_id)).ToList();
|
||||||
if (prod2.Count > 0)
|
if (prod2.Count > 0)
|
||||||
{
|
{
|
||||||
publicFun publicFun = new publicFun();
|
publicFun publicFun = new publicFun();
|
||||||
@@ -139,7 +139,7 @@ public class newsController : BaseApiController
|
|||||||
public IHttpActionResult GetList([FromBody] Model.ViewModel.news q, int page, int pageSize = 10,
|
public IHttpActionResult GetList([FromBody] Model.ViewModel.news q, int page, int pageSize = 10,
|
||||||
string sortBy = "", bool sortDesc = false, bool top = false)
|
string sortBy = "", bool sortDesc = false, bool top = false)
|
||||||
{
|
{
|
||||||
var qry = _db.news.AsEnumerable();
|
var qry = _db.news.AsQueryable();
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(q.subject))
|
if (!string.IsNullOrEmpty(q.subject))
|
||||||
qry = qry.Where(o => o.subject.Contains(q.subject.Trim()));
|
qry = qry.Where(o => o.subject.Contains(q.subject.Trim()));
|
||||||
@@ -258,10 +258,13 @@ public class newsController : BaseApiController
|
|||||||
else
|
else
|
||||||
qry = qry.OrderByDescending(o => o.topping).ThenByDescending(o => o.num);
|
qry = qry.OrderByDescending(o => o.topping).ThenByDescending(o => o.num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var count = qry.Count();
|
||||||
|
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
|
||||||
|
|
||||||
var ret = new
|
var ret = new
|
||||||
{
|
{
|
||||||
list = qry.ToPagedList(page, pageSize).Select(x => new
|
list = qryList.Select(x => new
|
||||||
{
|
{
|
||||||
num = x.num,
|
num = x.num,
|
||||||
subject = x.subject,
|
subject = x.subject,
|
||||||
@@ -282,7 +285,7 @@ public class newsController : BaseApiController
|
|||||||
topping = x.topping
|
topping = x.topping
|
||||||
|
|
||||||
}),
|
}),
|
||||||
count = qry.Count()
|
count = count
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -298,7 +301,7 @@ public class newsController : BaseApiController
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
var qry = _db.news_kind.AsEnumerable();
|
var qry = _db.news_kind.AsQueryable();
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(q.kind))
|
if (!string.IsNullOrEmpty(q.kind))
|
||||||
qry = qry.Where(o => o.kind.Contains(q.kind));
|
qry = qry.Where(o => o.kind.Contains(q.kind));
|
||||||
@@ -319,17 +322,18 @@ public class newsController : BaseApiController
|
|||||||
range = o.range,
|
range = o.range,
|
||||||
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
|
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
|
||||||
|
|
||||||
|
var count = qry.Count();
|
||||||
|
var qry2List = (pageSize > 0) ? qry2.ToPagedList(page, pageSize).ToList() : qry2.ToList();
|
||||||
|
|
||||||
var ret = new
|
var ret = new
|
||||||
{
|
{
|
||||||
list = qry2.ToPagedList(page, pageSize).Select(x => new
|
list = qry2List.Select(x => new
|
||||||
{
|
{
|
||||||
num = x.num,
|
num = x.num,
|
||||||
kind = new TreeView().RptDash(x.Level) + x.kind,
|
kind = new TreeView().RptDash(x.Level) + x.kind,
|
||||||
|
|
||||||
}),
|
}),
|
||||||
count = qry.Count()
|
count = count
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user