28
- July
2013
No Comments
Error: DbArithmeticExpression arguments must have a numeric common type.
From time to time you need to filter data by difference between two dates. For instance, you wanna take a difference between date from a record and Today.
Standard LINQ request will fail with the statement like the following:
1 2 3 |
var r = from e in Entity where e.Date.Subtract(DateTime.Today) <= 5; |
In Entity Framework version 4 the new utility class has been introduced – EntityFunctions. With the method of this class you can rewrite the statement above like this:
1 2 3 |
var r = from e in Entity where EntityFunctions.DiffDays(DateTime.Today, e.Date) <= 5; |
For someone who cannot use EntityFunctions for any reason there is a workaround. Long story short, you should compare the dates. The following changes might be done:
1 2 3 4 |
var maxDate = DateTime.Today.AddDays(5); var r = ... where e.Date <= maxDate; |