Using the IN operator as an alternative to OR

SearchExpressions in ADAM obviously support the OR-operator that allows you to run queries like this:

C#
1
2
3
4
SearchExpression expression = new SearchExpression(
  "Name = Joe or Email = joe@company.com");
users = new UserCollection(app);
users.Load(expression);

This is a perfectly valid way of writing queries. The problem is that we sometimes see partners writing code like this though:

C#
1
2
3
4
SearchExpression expression = new SearchExpression(
  "Name = Joe or Name = John or Name = Bill or Name = Gary");
UserCollection users = new UserCollection(app);
users.Load(expression);

Although this is also perfectly valid code, it is not the most performant way of doing things. For this purpose, ADAM also has a IN-operator which allows you do rewrite the previous code like this:

C#
1
2
3
4
SearchExpression expression = new SearchExpression(
  "Name in ?", new object[] { new string[]{ "Joe", "John", "Bill", "Gary" }});
UserCollection users = new UserCollection(app);
users.Load(expression);

The IN-operator only works when using parameters, but it does enable ADAM to generate a sql query that queries the Name-column only once compared to four times in the previous query. This SearchExpression works much faster than the SearchExpression using the OR operator, especially if you're using a lot (a few dozen for example) of OR operators.

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed