我有這個 linq 查詢:
var query = (from dc in context.Table1.Include(d => d.Doc)
join u in _context.Table2 on dc.IDDoc equals u.IDDoc
where dc.ID == id && u.IDUser == user.IDUser
select dc)
.Union(from dc in context.Table1.Include(d => d.Doc)
join p in _context.Table3 on dc.IDDoc equals p.IDDoc
where dc.ID == id
select dc);
我想添加更多條件動態取決于串列(List ids)我想要實作的是這樣的:
想象一下,我有一個 List ids = new(){1, 2, 5, 27); 我想要做的是將該資訊添加到查詢的這一部分中,以獲得如下內容:
.Union(from dc in context.Table1.Include(d => d.Doc)
join p in _context.Table3 on dc.IDDoc equals p.IDDoc
where dc.ID == id && p.ID == 1 || p.ID == 2 || p.ID == 5 || p.ID = 27
select dc)
但如果下次串列是 List ids = new(){4},查詢應該如下所示:
.Union(from dc in context.Table1.Include(d => d.Doc)
join p in _context.Table3 on dc.IDDoc equals p.IDDoc
where dc.ID == id && p.ID == 4
select dc)
甚至可能嗎?如果沒有,可能的解決方案是什么?謝謝
uj5u.com熱心網友回復:
使用Enumerable.Contains:
List<int> ids = new(){4};
....
.Union(from dc in context.Table1.Include(d => d.Doc)
join p in _context.Table3 on dc.IDDoc equals p.IDDoc
where dc.ID == id && ids.Contains(p.ID) // here
select dc)
....
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457178.html
