IN operator and LookupId

Mar 26, 2016 at 1:58 AM
I'm trying to use the IN operator with a LookupId, but it seems the IN operator will only work with integers or strings or simple datatypes.

What's the solution? Or do I have to use the OR operator instead?

Normally, syntax with ".Contains()" as below would work, but because the Contains extension method is looking for, in this case, int[], not LookupId[].

int[] lookupIds = {1, 2, 3};
Where(x => lookupIds.Contains((DataTypes.LookupId)x["LookupFieldTitle"]))
Mar 27, 2016 at 10:12 AM
PhillySPDev, it is in todo list at the moment:
  • Possibility to specify LookupId for In operator
At the moment you may specify LookupId by the following workaround:
string query = Camlex.Query().Where(…);
query = query.Replace("Name=\"Title\"", "Name=\"Title\" LookupId=\"True\"")
Depending on the name of your lookup field use it instead of "Title" in example above. Not very elegant, but should work.
Mar 28, 2016 at 5:17 AM
Edited Mar 28, 2016 at 5:18 AM
Yes, I had considered that possibility -- for a simple query, that is definitely an option. However, for my application, I was originally going to use the xml String argument, e.g. (string caml = Camlex.Query().WhereAll(xml, x => (int)x["Count"] == 1).ToString(); ) to combine the IN query with the rest of the query. Given the complexity of the query, and the potential presence of that field elsewhere in the query, I was uncomfortable with that work-around -- since I would have to run the String.Replace on the entire query.

Because, I found that if I performed a String.Replace as you suggest on the string that is the xml argument of the .WhereAll method, that Camlex would actually strip out the LookupId that I put placed in the code!

(P.S. Camlex is terrific, especially once one learns about the CombineOr method and others in ExpressionsHelper, which really open the door to dynamic querying).
Mar 29, 2016 at 4:05 PM
yep, I know that String.Replace() is not very good, but for a while this is the only option unfortunately. Suggest to use it only when there is no other way.