Skip to main content

If you've used Entity Framework in your software project you've might encounter the following error when you've tried to use datetime in ESQL query:

"The argument types 'Edm.DateTime' and 'Edm.String' are incompatible for this operation."

As we described using literals in Entity Framework ESQL in our article here, the solution for this error is to convert datetime/string to Entity Framework DateTime literal.

- Advertisement -
- Advertisement -

ESQL DateTime literal

A datetime literal is independent of locale and is composed of a date part and a time part. Both date and time parts are mandatory and there are no default values.

The date part must have the format: YYYY-MM-DD, where YYYY is a four digit year value between 0001 and 9999, MM is the month between 1 and 12 and DD is the day value that is valid for the given month MM.

The time part must have the format: HH:MM[:SS[.fffffff]], where HH is the hour value between 0 and 23, MM is the minute value between 0 and 59, SS is the second value between 0 and 59 and fffffff is the fractional second value between 0 and 9999999. All value ranges are inclusive. Fractional seconds are optional. Seconds are optional unless fractional seconds are specified; in this case, seconds are required. When seconds or fractional seconds are not specified, the default value of zero will be used instead.

There can be any number of spaces between the DATETIME symbol and the literal payload, but no new lines.

DATETIME'2012-10-01 23:15'

DATETIME'2012-12-25 01:01:00.0000000' -- same as DATETIME'2012-12-25 01:01'

ESQL query example

The code is intentionally simplified due to the purpose of this article.

...
 
ObjectQuery<Item> objectQuery = ItemEntities.CreateQuery<Item>("Item");
DateTime dateTimeValue = DateTime.Now;
 
// ESQL DATETIME format MUST be <yyyy-MM-dd HH:mm> format
objectQuery = objectQuery.Where(
            string.Format("(it.StartDate >= DATETIME'{0:yyyy-MM-dd HH:mm}')",
            dateTimeValue
);
 
...

- Advertisement -