修正多處 LINQ-to-Entities 查詢,避免 Nullable .Contains()、.ToString()、Request[] 直接使用造成翻譯失敗。

API 查詢同步改寫 .Contains()、.OrderBy()、複雜 GroupBy/Math.Round,必要時 materialize 或加 HasValue。
Participation rate / kind breakdown 改在記憶體計算,同時檢查整數陣列 .Contains() 的型別安全性。
This commit is contained in:
2025-11-14 23:40:55 +08:00
parent 4fcbfb3801
commit 27f916eb9c
14 changed files with 245 additions and 115 deletions

View File

@@ -319,7 +319,9 @@ public class activityController : ApiController
{
//var stockDt = _db.stocks.AsEnumerable(); ;//庫存
var fileDt = _db.files.Where(f => f.subject.Contains(q.fileTxt)).Select(f => f.num.ToString());//文件
// ❌ 錯誤寫法: var fileDt = _db.files.Where(f => f.subject.Contains(q.fileTxt)).Select(f => f.num.ToString());
// 改為整數陣列,避免後續查詢中使用 .ToString()
var fileDt = _db.files.Where(f => f.subject.Contains(q.fileTxt)).Select(f => f.num).ToArray();//文件
//每個品項在每個倉庫的結餘量
var stockDt = (
@@ -368,13 +370,22 @@ public class activityController : ApiController
if (q.category.HasValue && q.category.Value > 0)
qry = qry.Where(o => o.category == q.category.Value);
if (!string.IsNullOrEmpty(q.categorys))
qry = qry.Where(o => q.categorys.Contains(o.category.HasValue ? o.category.Value.ToString() : "0"));
{
// ❌ 錯誤寫法: qry = qry.Where(o => q.categorys.Contains(o.category.HasValue ? o.category.Value.ToString() : "0"));
// LINQ to Entities 無法轉換資料庫欄位的 .ToString(),改為整數陣列比較
var categoryArray = q.categorys.Split(',').Select(x => int.TryParse(x, out int result) ? result : 0).ToArray();
qry = qry.Where(o => categoryArray.Contains(o.category ?? 0));
}
if (!string.IsNullOrEmpty(q.status))
qry = qry.Where(o => o.status.Contains(q.status));
if (!string.IsNullOrEmpty(q.extend))
qry = qry.Where(o => o.extend == q.extend);
if (!string.IsNullOrEmpty(q.fileTxt))
qry = qry.Where(o => o.actItem_files.Where(f2 => f2.actItem_num == o.num && fileDt.ToArray().Contains(f2.files_num.ToString())).Count() > 0);
{
// ❌ 錯誤寫法: qry = qry.Where(o => o.actItem_files.Where(f2 => f2.actItem_num == o.num && fileDt.ToArray().Contains(f2.files_num.ToString())).Count() > 0);
// fileDt 已改為整數陣列,直接比較即可
qry = qry.Where(o => o.actItem_files.Where(f2 => f2.actItem_num == o.num && fileDt.Contains(f2.files_num)).Count() > 0);
}
//qry = qry.Where(o => o.actItem_files.Where(f2 => f2.actItem_num == o.num && f2.files_num.ToString().Contains( String.Join(",", fileDt.Select(p => p.ToString()).ToArray()) )).Count() >0 ) ;
if (sortBy.Equals("subject"))
@@ -581,7 +592,10 @@ public class activityController : ApiController
qry = qry.Where(o => o.actItem_kind.kind.Contains(q.kindTxt));
if (!string.IsNullOrEmpty(q.is_reconcile))
qry = qry.Where(o => o.is_reconcile == q.is_reconcile);
qry = qry.Where(o => "1,2,4".Contains(o.category.HasValue ? o.category.Value.ToString() : "0")); //報名,掛單,贊助
// ❌ 錯誤寫法: qry = qry.Where(o => "1,2,4".Contains(o.category.HasValue ? o.category.Value.ToString() : "0"));
// LINQ to Entities 無法轉換資料庫欄位的 .ToString(),改為整數陣列比較
var allowedCategories = new[] { 1, 2, 4 };
qry = qry.Where(o => allowedCategories.Contains(o.category ?? 0)); //報名,掛單,贊助
qry = qry.OrderByDescending(o => o.num);
var count = qry.Count(); //pageSize = count;//一次取回??
var qryList = (pageSize > 0) ? qry.ToPagedList(page, pageSize).ToList() : qry.ToList();
@@ -1377,6 +1391,9 @@ public class activityController : ApiController
}
else if (sortBy.Equals("reg_time_time"))
{
// ⚠️ 警告: TimeOfDay 屬性在某些 Entity Framework 版本中可能不支援
// 如果發生 System.NotSupportedException需改為先 ToList() 再排序
// 目前如果正常運作,則資料庫提供者支援此操作
if (sortDesc)
qry = qry.OrderByDescending(o => o.reg_time.Value.TimeOfDay);
else