Linq-动态构建LINQ查询时模拟OrWhere表达式吗?

问题描述:

搜索下面的代码段允许用户将字符串与表中的三个字段进行匹配.如果任何字段匹配,则该条目将包括在结果中.但是,使用位置"筛选结果会导致字符串必须匹配所有三个字段",而不是字符串可以匹配三个字段中的任何一个".

The code snippet below search allow the user to match a string against three fields in the table. If any of the fields match, the entry is included in the result. However, using Where to filter out the results is resulting in "the string must match all three fields" instead "the string can match any of the three fields".

动态构建LINQ查询时是否可以模拟OrWhere表达式?

Is there a way to simulate an OrWhere expression when building LINQ queries dynamically?

var foundUsers = from UserInfo user in entities.UserInfo
                 select user;

if (searchCompleteName)
{
    foundUsers = foundUsers.Where(u => u.CompleteName.Contains(searchString));
}

if (searchPortalID)
{
     foundUsers = foundUsers.Where(u => u.PortalID.Contains(searchString));
}

if (searchUsername)
{
     foundUsers = foundUsers.Where(u => u.UserIdentity.Contains(searchString));
}

PS.我正在使用Entities Framework和LINQ to Entities,并且正在做MVC3 Web应用程序.

PS. I am using Entities Framework and LINQ to Entities, and am doing a MVC3 Web Application.