LINQ DB Arithmetic must have common type (Date Comparison
asp.net-mvc, c#, linq, sql
Solution
You cannot use subtraction in a query: the `operator -` is overloaded in .NET, but not in the SQL Server.
You can use `SqlFunctions.DateDiff` to achieve the effect that you are looking for:
var maxDaysTask = whseTasks
.Where(x => x.CompDate != null && x.TaskDate != null)
.Max(x => SqlFunctions.DateDiff("day", x.TaskDate, x.CompDate));
Problem
I am trying to get the record from a database with the highest amount of days using a start and end date. I keep getting a System.Argument.Exception in the Entity.dll. The error states: ``` {"DbArithmeticExpression arguments must have a numeric common type."} ``` Here is my LINQ statement: ``` var maxDaysTask = whseTasks.Where(x => x.CompDate != null && x.TaskDate != null).Max(x => ((DateTime)x.TaskDate - (DateTime)x.CompDate).TotalDays); ``` The CompDate(end date) and TaskDate(begin date) are both nullable datetimes. Any advice?