This is certainly an XY problem if I ever saw one. You're asking about comparing datetime as strings in Entity Framework, while it seems the real problem is that you don't like the default date format of the DataContractJsonSerializer
that WCF uses by default.
Part of the problem is that you are mixing local and UTC. You're getting /Date(1380924000000+0200)/
, which contains the local time zone offset of the server. This is because you started from DateTime.Now
, which has a .Kind
of DateTimeKind.Local
.
If you instead used DateTime.UtcNow
, it would have a .Kind
of DateTimeKind.Utc
, and would be serialized as /Date(1380924000000)/
. And yes, the numerical portion of the format would be the same. Even when there is an offset specified, the number part is still related to UTC.
That's just one problem with this format. The other is that while DataContractJsonSerializer
writes the local offset during serialization, it doesn't use it properly during deserialization. It just assumes that if any offset is provided, that the time should be local - even if the computer doing the deserialization has a completely different offset.
The better format to use in JSON is the ISO8601 format. For example, this UTC value would look like 2013-10-04T22:00:00.000Z
. And while you can easily pass in a different date format to DataContractJsonSerializer
if you use it directly, WCF doesn't easily expose this to you.
You could go down the route of changing the DataContractJsonSerializer
settings via a custom WCF message formatter, such as described here, here and here, but it gets complicated very quickly. Be careful if you do this, and be sure to test thoroughly!
Another idea would be to write a custom WCF message formatter that uses JSON.Net instead of DataContractJsonSerializer
. JSON.Net uses the ISO8601 format by default, so you would be set.
But honestly, the best solution is to not try to use WCF to build your REST endpoints. The date format issue is just the beginning. There are all sorts of other problems that can pop up along the way. Instead, use a modern framework that is designed for this purpose, such as ASP.Net WebAPI, or ServiceStack.
ASP.Net WebAPI uses JSON.Net, and ServiceStack has it's own JSON serializer called ServiceStack.Text. JSON.Net uses ISO8601 as it's default date format. If you use Service Stack, you'll need to set JsConfig.DateHandler = JsonDateHandler.ISO8601;
So in recap, you have two XY problems:
- First, you chose WCF to build your rest endpoints, which was so problematic that the industry developed other solutions.
- Second, you couldn't get
DateTime
to emit the correct format, so you decided to treat it as a string, which then you couldn't compare back to a DateTime
in an EF query.
And yes, I realize I didn't answer your question of how to compare a DateTime
input against a string property in Entity Framework, but I think now you can see why. If you really want to go down that road, you might find something useful in SqlFunctions
or EntityFunctions
- but even then, how are you going to build an efficient index on this column in your database? Querying will be really slow if you get a lot of data. I would advise against it.