String comparing on greater / less than value

Jan 20, 2016 at 2:10 AM
We'd like to compare strings as per the following query:

x => x["DocDate"] >= from && x["DocDate"] <= to

from / to are strings, so we are getting "Operator '>=' cannot be applied to operands of type 'object' and 'string'"

The following one goes the sa,me way:

x => (string)x["DocDate"] >= from && (string)x["DocDate"] <= to

Is there any way to overcome that?
Jan 20, 2016 at 2:13 AM
Alright, will the following be the right assumption?

x => (DataTypes.Text)x["DocDate"] >= (DataTypes.Text)from && (DataTypes.Text)x["DocDate"] <= (DataTypes.Text)to
Coordinator
Jan 20, 2016 at 8:15 AM
Hi Anton,
in this case there won't be compilation error, but this expression is not valid currently in Camlex, and I'm not sure that it should be valid if even compiler doesn't allow it. I understand that in real life there may be situations when you need to compare strings in lexicographical order, but it should be done carefully, because often it leads to unpredictable errors especially when you compare strings which represent dates. Can't you use normal DateTime type for the filter?
Jan 21, 2016 at 12:22 AM
Edited Jan 21, 2016 at 12:23 AM
Understand that.
lexicographical order ... specially when you compare strings which represent dates
Exactly the case, pre-1900 dates case :)

The following one does the job:
x => x["DocDate"] >= (DataTypes.Text)from && x["DocDate"] <= (DataTypes.Text)to

Thanks for the tips!