Geeks With Blogs
Adrian Hara Working through the .NET maze

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 | Back to top


Comments on this post: How to OR multiple conditions in (d)Linq

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
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.
Left by Peter G. on Aug 03, 2007 2:12 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
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 :)
Left by Adrian Hara on Aug 03, 2007 9:32 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
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
Left by Robin Thomas on Aug 20, 2008 9:02 PM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
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%' ..."
Left by Adrian Hara on Aug 22, 2008 7:48 PM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
you can Or them with Union method
union above queries
Left by Faezeh on Jan 12, 2010 12:47 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
Right, but in case of Linq2Sql I think this translates to

Query
UNION
Query

...instead of just one query with OR conditions, which should be faster
Left by Adrian Hara on Jan 12, 2010 1:01 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
Hello Adrian, I am on this point too. How to compose a query in case of AND/OR not known, in terms of number, until runtime. Your first example should be ok for AND conditions, even if it forces 2 or more queries to be sent, rather than 1 with the complete clause. Same with "Union", in case of several conditions what's the processing time (more queries + aggregation) ?
I understand the power of LINQ, with integrated query. But how to compose them, as we did with query strings ?
Have you had any progress with this ?
Max
Left by Maximilian on Feb 28, 2010 8:27 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
Max: did you try the link i posted at the end? This one: http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/925b245d-5529-4a64-8cd4-4bc83ee6fe7a/
Left by Adrian Hara on Feb 28, 2010 10:00 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
yes I did, but that coding seems to me still very abstract and not intuitive...is it possible that just to use some OR in our query, the simplest thing in the world, we have to use such predicates or expression trees? it really makes me smile and use my old sql queries string....
Left by Max on Mar 01, 2010 1:20 PM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
Well, I haven't used Linq2Sql in a while now, but as far as I know there is no other easier way to do it...
Left by Adrian Hara on Mar 02, 2010 1:03 AM

# re: How to OR multiple conditions in (d)Linq
Requesting Gravatar...
Thanks for your responses, Adrian. It just seems to me they have lost the point here. My little opinion is that it can be an obstacle to the diffusion of this technology, at least for me. It was just enough if the designers allowed us to use "if" conditions inside the query being built.
Left by Max on Mar 02, 2010 10:47 AM

Your comment:
 (will show your gravatar)


Copyright © Adrian Hara | Powered by: GeeksWithBlogs.net