Mix AND and OR with expressions

Jan 10, 2012 at 11:34 AM


First I want to thanks all the team that is working on Camlex.NET because I find it really great!!! CAML is a pain to write but with Camlex it becomes a pleasure :-).

I'm facing a problem mixing AND and OR in expressions.

Here is my code:

var expressions = new List<Expression<Func<SPListItem, bool>>>();

expressions.Add(x => ((string)x["Moderators"]).Contains(currentUserLoginName));

List<string> entities = myEntities;

var query = Camlex.Query().WhereAll(expressions).ToString();

I want to do some foreach on my "entities" and add the following "dynamic" expression (that is not dynamic right now :-P):

expressions.Add(x => ((string)x["Entity"]).Contains(entities[0]) || ((string)x["Entity"]).Contains(entities[1]) || ((string)x["Entity"]).Contains(entities[2]));

As I don't know how many "entities" I have I can not just hardcode it.

Does it give some syntax to do this?

I tried something like that, but the compiler doesn't like it...

expressions.Add(x =>
                       foreach (var entity in entities)
                           ((string) x["Entity"]).Contains(entity);

Thanks for help ;-).

Jan 10, 2012 at 11:53 AM

hi PlaTyPuS,

I wrote a post about it recently (there were several similar questions about how to use both OR and AND operations). Check it here: Create complex dynamic CAML queries with Camlex.Net. There is ExpressionsHelper class which you can use in order to create queries which contains both OR and AND. Details are in the blog post. If you will still need questions after it, post them here - will try to help.

Jan 10, 2012 at 1:17 PM


This is what I call fast-support :-).

Thanks it's exactly what I was looking for!