2017-04-12 89 views
2

我想對單詞列表執行查詢。MVC .NET OrWhere查詢

string[] words = {"word1", "word2", "word3"}

在我的數據庫我有藝術家和標題曲目列表。我想連接所有的查詢與OrWhere條件。 這是我的時刻:

var word; 
var queryTracks; 
for (int i = 0; i < words.Length; i++) 
{ 
    word = words[i]; 
    queryTracks = db.Tracks.Where(c => c.Title.Contains(word) || c.Artist.Contains(word)); 
} 
// all found, put all that into a list 
var filteredTracks = queryTracks.ToList(); 

我已經與.Concat()方法是這樣的嘗試:

var word = words[0]; 
var queryTracks = db.Tracks.Where(c => c.Title.Contains(word) || c.Artist.Contains(word));; 
for (int i = 1; i < words.Length; i++) 
{ 
    word = words[i]; 
    queryTracks = queryTracks.Concat(db.Tracks.Where(c => c.Title.Contains(word) || c.Artist.Contains(word))); 
} 

但它沒有工作...我只是想將所有的跟蹤word1,word2和word3。我可以做一個大的查詢,然後把它放到一個列表中,或者我必須使list1,list2和list3連接起來嗎?

回答

6

您可以使用一個單一的查詢

string[] words = {"word1", "word2", "word3"} 

IEnumerable<Track> tracks = db.Tracks.Where(c => words.Contains(c.Title) || words.Contains(c.Artist)); 
+0

這是......那麼容易,謝謝! – Didi