This project has moved. For the latest updates, please go here.

Where statement question

Jun 17, 2010 at 3:24 AM

Hey Camlex.Net experts. I have a where clause question for you. I've reviewed the samples you provided on the Scenario 2. Query with “and”/”or” conditions.  However, I have a situation where I wanted to do something like this...

where(x=> x[field]=="something" && x[field] != null && (x[field] == "A" || x[field] == "B" || x[field] == "C"))

But it said it can't parse this into CAML. How would you recommend handling this with CAML? Is it possible?

Coordinator
Jun 17, 2010 at 7:48 AM

hello, chorpeac

the above expression should be slightly modified - you need to add explicit cast to string every time you use string type, i.e.:

 

string field = "Title";
var c =
    Camlex.Query().Where(
        x =>
        (string)x[field] == "something" && x[field] != null &&
        ((string)x[field] == "A" || (string)x[field] == "B" || (string)x[field] == "C")).ToString();

Console.WriteLine(c);

 

 

it will produce desired CAML:

 

<Where>
  <And>
    <And>
      <Eq>
        <FieldRef Name="Title" />
        <Value Type="Text">something</Value>
      </Eq>
      <IsNotNull>
        <FieldRef Name="Title" />
      </IsNotNull>
    </And>
    <Or>
      <Or>
        <Eq>
          <FieldRef Name="Title" />
          <Value Type="Text">A</Value>
        </Eq>
        <Eq>
          <FieldRef Name="Title" />
          <Value Type="Text">B</Value>
        </Eq>
      </Or>
      <Eq>
        <FieldRef Name="Title" />
        <Value Type="Text">C</Value>
      </Eq>
    </Or>
  </And>
</Where>

 

It was made by design because at first Visual Studio and ReSharper show warning when expression "x[field] == "something"" is used without cast to string: "Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'". And at second there can be queries with BeginsWith and Contains operations for strings (see Scenario 4 at http://camlex.codeplex.com) where cast to string is required as StartsWith and Contains methods are defined in in .Net string type - and in order to make things using the same single approach, explicit cast to string was made required operation in all operations with strings.