Linq-动态构建LINQ查询时模拟OrWhere表达式吗? [英] Linq - Simulate an OrWhere expression when building LINQ queries dynamically?

查看:116
本文介绍了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.

推荐答案

尝试以下操作:- http: //www.albahari.com/nutshell/predicatebuilder.aspx

这篇关于Linq-动态构建LINQ查询时模拟OrWhere表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆