使用if if with LINQ Where

Has*_*san 1 linq asp.net-mvc-2

我想生成动态查询来检查管理where子句与可用参数的数量...如果某个参数为null我不想将它包含在where子句中

var test = from p in _db.test
           where if(str1 != null){p.test == str} else i dnt wanna check p.test
Run Code Online (Sandbox Code Playgroud)

我有大约14个参数用于where子句

需要帮助,谢谢

Fre*_*örk 5

你可以分步完成:

// set up the "main query"
var test = from p in _db.test select _db.test;
// if str1 is not null, add a where-condition
if(str1 != null)
{
    test = test.Where(p => p.test == str);
}
Run Code Online (Sandbox Code Playgroud)