Adrian Hara

Working through the .NET maze

  Home  |   Contact  |   Syndication    |   Login
  42 Posts | 0 Stories | 83 Comments | 10 Trackbacks

News

Archives

Post Categories

I've been playing a bit with (d)Linq lately so there are probably some related blog posts coming this way :).

Today, for example, I was trying to get the logic for a search form to work. The form is pretty basic, allowing the user to search a books library based on several fields, like Title, Author, Publisher etc. The search should tokenize the input from each field and then build the final condition by OR-ing the conditions for each field together, provided that something was entered in that search field to start with. For example one could search for Title containing "patterns design" OR Author containing "Gamma". Should be simple with (d)Linq, right? Almost... :)

If I wanted to do a similar search to the one described above, but instead of OR-ing the conditions, AND-ing them (ie Title contains "patterns" AND Author contains "Gamma") this is pretty straightforward, along the lines of maybe (no tokenizing):

IQueryable<Book> query = from b in db.Books select b;

if (!string.IsNullOrEmpty(title))

{

   query = from b in query

   where b.Title.Contains(title)

   select b;

}

if (!string.IsNullOrEmpty(author))

{

   query = from b in query

   where b.Author.Contains(author)

   select b;

} 

… and so on

So what about OR-ing? Well, to be honest I thought of a bad solution :), but then found a little utility class by Joe Albahari (scroll down to the last posts on that page), which does some magic and gets stuff to work. VERY cool and neat!

Also, if you happen to know of another way to do this, please share...

posted on Tuesday, February 20, 2007 6:02 PM

Feedback

# re: How to OR multiple conditions in (d)Linq 8/3/2007 2:12 AM Peter G.
For AND'ing multiple where conditions use "&&" between the conditions.

For OR-ing multiple where conditions use "||" between the conditions.

This derives from lambda expressions.

# re: How to OR multiple conditions in (d)Linq 8/3/2007 9:32 AM Adrian Hara
Ok, that's fine when you know the conditions beforehand, but how do you do that when the number of conditions is not known until runtime? I don't know of another way except Joe's I mentioned. But I admit I'm not really up with current Linq changes :)

# re: How to OR multiple conditions in (d)Linq 8/20/2008 9:02 PM Robin Thomas
IQueryable<Book> query = from b in db.Books select b;

query = from b in query
select b;

if (!string.IsNullOrEmpty(title))

{
query = query.where(col => col.Title.Contains(title));
}

if (!string.IsNullOrEmpty(author))

{
query = query.where(col => col.Author.Contains(author));
}
… and so on

# re: How to OR multiple conditions in (d)Linq 8/22/2008 7:48 PM Adrian Hara
Right, but unless I'm missing something, this AND-s the conditions together. My post was about OR-ing them so the final query looks something like:

"get books which have title like '%foo%' OR author like '%bar%' ..."

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: