Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - Simulate an OrWhere expression when building LINQ queries dynamically?

Tags:

linq

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".

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. I am using Entities Framework and LINQ to Entities, and am doing a MVC3 Web Application.

like image 662
Extrakun Avatar asked Mar 25 '11 05:03

Extrakun


People also ask

What is LINQ expression?

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.

Is Linq a query builder?

dynamic-linq-query-builder is a small library that allows any . Net framework class collection to be filtered dynamically at runtime.

What is predicate in Linq C#?

A predicate, or more precisely a predicate functor, is a Boolean-valued function. It is often a unary function, checking one argument against a condition and returning the result. Disregarding functions depending on side effects, there are two nullary functions (without arguments). public static class Predicate.


2 Answers

Try this:- http://www.albahari.com/nutshell/predicatebuilder.aspx

like image 69
Ian Mercer Avatar answered Jan 12 '23 03:01

Ian Mercer


Not exactly pretty, but it would work.

var foundUsers = entities.UserInfo.Where(u =>
    (searchCompleteName && u.CompleteName.Contains(searchString))
    || (searchPortalID && u.PortalID.Contains(searchString))
    || (searchUsername && u.UserIdentity.Contains(searchString));

You could also do this with a union. The union operator returns distinct results so there will not be any duplicates. I have no idea if EF can defer this to the database.

var foundUsers = Enumerable.Empty<UserInfo>().AsQueryable();

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

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

if (searchUsername)
{
     foundUsers = foundUsers.Union(entities.UserInfo.Where(u => u.PortalID.Contains(searchString)));
}
like image 42
Ryan Avatar answered Jan 12 '23 04:01

Ryan