MVC .NET OrWhere查询

时间:2017-04-12 01:59:13

标签: c# sql asp.net-mvc entity-framework dbcontext

我想对单词列表执行查询。

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并将它们连接起来?

1 个答案:

答案 0 :(得分:6)

您可以使用单个查询

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

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