Today I released new 3.6 version of Camlex open source library (and Camlex.Client 1.5 for managed client applications). For those who doesn’t know about this project yet, it is library which helps to build dynamic CAML queries (i.e. queries which are built in runtime based on some parameters) via C# lambda expressions and fluent interfaces. You may see a lot of examples on this page. Also in my blog you may see other related articles about Camlex.
In new release support for explicit cast boolean expressions was added. I.e. if before you have to write expression like this:
1: string caml = Camlex.Query().Where(x => (bool)x["Foo"] == true).ToString();
which in turn produces the following CAML:
1: <Where>
2: <Eq>
3: <FieldRef Name="Foo" />
4: <Value Type="Boolean">1</Value>
5: </Eq>
6: </Where>
then now you may write in simpler way without comparing with true or false explicitly:
1: string caml = Camlex.Query().Where(x => (bool)x["Foo"]).ToString();
This record is shorter and won’t cause problems with ReSharper which may suggest you to remove not needed from its point of view comparison (in previous version it would cause exception).
In order to produce CAML with comparing with false value, use not (!) operator:
1: string caml = Camlex.Query().Where(x => !(bool)x["Foo"]).ToString();
which will produce:
1: <Where>
2: <Eq>
3: <FieldRef Name="Foo" />
4: <Value Type="Boolean">0</Value>
5: </Eq>
6: </Where>
The same simplified syntax is available in complex conditions with && and || operations:
1: string caml = Camlex.Query().Where(x => ((bool)x["foo1"] && !(bool)x["foo2"]) ||
2: (bool)x["foo3"]).ToString();
will produce:
1: <Where>
2: <Or>
3: <And>
4: <Eq>
5: <FieldRef Name="Foo1" />
6: <Value Type="Boolean">1</Value>
7: </Eq>
8: <Eq>
9: <FieldRef Name="Foo2" />
10: <Value Type="Boolean">0</Value>
11: </Eq>
12: </And>
13: <Eq>
14: <FieldRef Name="Foo3" />
15: <Value Type="Boolean">1</Value>
16: </Eq>
17: </Or>
18: </Where>
Also appropriate change was added to reverse engineering feature which allows to get C# representation of CAML expression with Camlex library (i.e. opposite transformation comparing with what is shown above). I.e. if you will enter the last CAML query above in Camlex online service, you will also get short version of boolean cast expressions without explicit comparison with true or false. Camlex online service was created exactly for simplifying using of Camlex.Net library for developers which worked with regular string CAML queries before.
New packages can be downloaded from Codeplex project site or from NuGet with the following commands:
Install-Package Camlex.NET.dll
Install-Package Camlex.Client.dll
If you have any comments or ideas for further improvements don’t hesitate to post them on discussions page on project site.
No comments:
Post a Comment