我想在用戶輸入文本框中的關鍵字時向用戶提供數據庫表中的項目列表。我現在遇到的問題是用戶可以輸入多個關鍵字,並且我希望能夠僅執行一次往返數據庫並返回匹配多個關鍵字(一個OR操作)的結果。有沒有辦法執行這樣的任務,而不必每個關鍵字都擊中數據庫?「Where In」with linq to sql
0
A
回答
1
我發現了一個post,提供一個漂亮很好的解決方案,我試圖實現。感謝您的幫助!
0
我發現this page當試圖弄清楚LINQ
在C#中的東西時非常有用。這表明the following syntax:
var results = from Item i in myCollectionOfItems
where i.OneThing = aGoodThing || i.SomeStuff != theBadStuff
select i;
編輯:對不起,誤解了這個問題 - 我已經更新,以匹配一個OR
操作請求。 (我找不到參照頁||
語法,但我相信它會奏效。它compiles on my machine ...
5
載有()是你的朋友。
List<string> keywords = new List<string>();
keywords.Add("foo");
keywords.Add("bar");
var results = from item in db.Table
where keywords.Contains(item.Description)
select item;
...給你
WHERE [t0].[Description] IN (@p0, @p1)
0
記號化你的輸出你的關鍵字搜索。
那麼你可以不斷地說或者在WHERE條件C#(||運算符)。
即
var query = from row in mytable
where (row.column == a) || (row.column == b) || //.... etc
select row
0
你可以試試:
List<string> keywords = new List<string>();
keywords.Add("foo");
keywords.Add("bar");
var results = from item in db.Table
where keywords.Exists(k => item.Description.Contains(k))
select item;
但我不知道它會轉換成SQL ...
相關問題
- 1. LINQ to SQL WHERE「IN」
- 2. LINQ to SQL - How to「Where ... in ...」
- 3. LINQ-to-SQL IN()
- 4. LINQ to Dataset - 相當於sql(where ... in ...)
- 5. IQueryable動態「WHERE IN」(LINQ to SQL)
- 6. LINQ to SQL in and not in
- 7. Signin in Linq to Sql?
- 8. linq to SQL OnLoaded()with SQL View?
- 9. linq to sql「into」with select
- 10. LINQ to EF4中的「Where Not In」
- 11. LINQ to SQL with Unity Interception
- 12. SQL where field in vs. where field = with multiple or?
- 13. LINQ to SQL與where case case
- 14. Linq to SQL with REST API?
- 15. Linq to SQL with Stored Procedures
- 16. LINQ to SQL lambda exp。 OrderBy,Case When,Where Where
- 17. .Include().Where()in Linq to Entities查詢
- 18. Linq to SQL加入和Where
- 19. LINQ to SQL in Visual Studio,InvalidCastException with no call stack
- 20. DefaultIfEmpty - LINQ to SQL vs In Memory
- 21. Linq to SQL to ObservableCollection for treeview in VB.Net
- 22. LINQ to Entities等價於SQL NOT IN
- 23. 在LINQ to SQL中選擇「IN」
- 24. LINQ to EF - 模擬SQL「IN」子句
- 25. SelectMany in Linq to entity
- 26. LINQ to SQL INSERT WHERE不在集合中
- 27. LINQ to SQL MAX在WHERE子句中
- 28. LINQ to SQL - 分組依據/ Where
- 29. String.Replace in LINQ to Entities
- 30. OrderBy in LINQ to Entity
如果您想查看描述中是否包含關鍵字... – 2009-06-07 00:10:15
,那麼這將不起作用我想在Google提示框的行中添加一些內容。關鍵字不一定是完美匹配 – ak3nat0n 2009-06-07 00:50:15