Complex queries

Aug 10, 2010 at 5:12 PM

Hi,

I just stumbled across Camlex.Net and this looks really cool - looks just what I might need for my current requirements.

I have a list of values which I need to query using the WhereAny method. Unfortunatly I also have to add an additional constrain to that clause. So the final Caml should look like this:

<Where>
  <And>
    <Or>
      <Or>
        <Eq>
          [...]
    </Or>
    <Eq>
      [...]
    </Eq>
  </And>
</Where>
      

(I hope you get the idead).

How could I accomplish this using Camlex? So I basically want to have a list of expressions which are or-joined and at least a single expression added as "and" (migth be more than just on additional expression). Is there any way to achiev this kind of nesting?

Developer
Aug 11, 2010 at 8:18 AM

Right now, it is not supported.

But the appropriate implementation for this task is rather simple - we just have to expose some internal method grouping list of extensions. So it will look like:

  1. you call CombineOr(Expression[]) with list of expressions - it returns the single expression.
  2. then you call CombineAnd(Expression[]) with two expressions: the first comes from the point 1 and the second is additional constraint. The method returns the single expression.
  3. then you call Camlex class with the expression coming from point 2 in usual way.

It is simple - so we will implement it by this evening.

Aug 11, 2010 at 9:08 AM
That sounds perfect! I'm really looking forward to incorporate this into my app! Thx alot! I really appreciate your work!
Developer
Aug 11, 2010 at 3:55 PM

Implemented.

Example of usage:

            var expressionsList = new List<Expression<Func<SPListItem, bool>>>
            {
                x => (int) x["ID"] == 1,
                y => (int) y["ID"] == 2,
                z => (int) z["ID"] == 3
            };
            Expression<Func<SPListItem, bool>> additionalExpression = (w => (string)w["Title"] == "Test");

            var combinedExpressions = ExpressionsHelper.CombineOr(expressionsList);
            var finalExpression = ExpressionsHelper.CombineAnd(new[] {combinedExpressions, additionalExpression});
            var caml = Camlex.Query().Where(finalExpression).ToString();

            var expected =
                "   <Where>" +
                "       <And>" +
                "           <Or>" +
                "               <Or>" +
                "                   <Eq>" +
                "                       <FieldRef Name=\"ID\" />" +
                "                       <Value Type=\"Integer\">1</Value>" +
                "                   </Eq>" +
                "                   <Eq>" +
                "                       <FieldRef Name=\"ID\" />" +
                "                       <Value Type=\"Integer\">2</Value>" +
                "                   </Eq>" +
                "               </Or>" +
                "               <Eq>" +
                "                   <FieldRef Name=\"ID\" />" +
                "                   <Value Type=\"Integer\">3</Value>" +
                "               </Eq>" +
                "           </Or>" +
                "           <Eq>" +
                "               <FieldRef Name=\"Title\" />" +
                "               <Value Type=\"Text\">Test</Value>" +
                "           </Eq>" +
                "       </And>" +
                "   </Where>";
Aug 12, 2010 at 1:11 PM
works like a chram! Thanx!