以下内容将选择包含两者 FirstName和LastName
的关键字的人 if (searchFields.Contains("FirstName"))
{
query = query.Where(p => p.FirstName.Contains(keyword));
}
if (searchFields.Contains("LastName"))
{
query= query.Where(p => p.LastName.Contains(keyword));
}
如何更改它,以便选择 FirstName或LastName包含关键字的人?
换句话说,我正在寻找OR条款,但我不知道该怎么做
答案 0 :(得分:3)
bool searchFirstName = searchFields.Contains("FirstName");
bool searchLastName = searchFields.Contains("LastName");
if (searchFirstName && searchLastName)
{
query = query.Where(p => p.FirstName.Contains(keyword)
|| p.LastName.Contains(keyword));
}
else if(searchFirstName)
{
query = query.Where(p => p.FirstName.Contains(keyword));
}
else if(searchLastName)
{
query = query.Where(p => p.LastName.Contains(keyword));
}
答案 1 :(得分:0)
这就是我需要的:
http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx
它正在使用LINQ表达式,它使用Expression.Or
组合它们