【发布时间】:2010-10-21 09:45:37
【问题描述】:
我有一个可变大小的字符串数组,我试图以编程方式循环遍历数组并匹配表中的所有行,其中“标签”列至少包含数组中的一个字符串。这是一些伪代码:
IQueryable<Songs> allSongMatches = musicDb.Songs; // all rows in the table
我可以轻松地在一组固定的字符串上查询此表过滤,如下所示:
allSongMatches=allSongMatches.Where(SongsVar => SongsVar.Tags.Contains("foo1") || SongsVar.Tags.Contains("foo2") || SongsVar.Tags.Contains("foo3"));
但是,这不起作用(我收到以下错误:“无法将带有语句体的 lambda 表达式转换为表达式树”)
allSongMatches = allSongMatches.Where(SongsVar =>
{
bool retVal = false;
foreach(string str in strArray)
{
retVal = retVal || SongsVar.Tags.Contains(str);
}
return retVal;
});
谁能告诉我正确的策略来完成这个?我对 LINQ 的世界还是新手 :-)
【问题讨论】:
标签: linq linq-to-sql dynamic