Adrian Hara

Working through the .NET maze

  Home  |   Contact  |   Syndication    |   Login
  46 Posts | 0 Stories | 108 Comments | 10 Trackbacks

News

Twitter












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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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%' ..."

# re: How to OR multiple conditions in (d)Linq 1/12/2010 12:47 AM Faezeh
you can Or them with Union method
union above queries

# re: How to OR multiple conditions in (d)Linq 1/12/2010 1:01 AM Adrian Hara
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

# re: How to OR multiple conditions in (d)Linq 2/28/2010 8:27 AM Maximilian
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


# re: How to OR multiple conditions in (d)Linq 2/28/2010 10:00 AM Adrian Hara
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/

# re: How to OR multiple conditions in (d)Linq 3/1/2010 1:20 PM Max
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....

# re: How to OR multiple conditions in (d)Linq 3/2/2010 1:03 AM Adrian Hara
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...

# re: How to OR multiple conditions in (d)Linq 3/2/2010 10:47 AM Max
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.

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