我有一個主表“SALES”和兩個副表“PRODUCTS”和“SERVICES”,我只需要選擇“SALES”中包含用戶輸入的一些產品或服務的記錄,我不需要帶銷售記錄和產品,只需過濾即可。首先,我在“銷售”表中按銷售日期制作了過濾器:
var query = (from p in _contexto.sales
where p.datesale.Value.Date >= Convert.ToDateTime(strDtI).Date &&
p.datesale.Value.Date <= Convert.ToDateTime(strDtF).Date
select p);
現在假設用戶還想過濾具有字串陣列中單詞的產品或服務的銷售
words = ['apple', 'beef', 'cleaning', 'haircut']
如果您收到單詞陣列,我嘗試了下面的過濾器,但它不起作用,它一直在帶來所有記錄。
var queryi = (from i in _contexto.products
where words.Contains(i.name) || words.Contains(i.description) select i);
//var queryj = (from i in _contexto.services
//where words.Contains(i.name) || words.Contains(i.description) select i);
//query = query.Where(p => queryi.All(c => c.idsale != p.id) || queryj.All(c => c.idsale != p.id));
query = query.Where(p => queryi.All(c => c.idsale != p.id));
我在哪里失敗了,有沒有更好、更高效的方法來做到這一點?謝謝!
uj5u.com熱心網友回復:
使用更具描述性的變數名稱,并假設您只想查找與其中一個名稱或描述完全相同的產品words,您將擁有:
var salesInPeriod = from s in _contexto.sales
where Convert.ToDateTime(strDtI).Date <= s.datesale.Value.Date &&
s.datesale.Value.Date <= Convert.ToDateTime(strDtF).Date
select s;
var matchingidsales = from p in _contexto.products
where words.Contains(p.name) || words.Contains(p.description)
select p.idsale;
var ans = from s in salesInPeriod
where matchingidsales.Contains(s.id)
select s;
PS:我顛倒了日期比較,因為我認為它可以更容易地看到你正在做一個測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470992.html
