When I am trying to run this command using Entity framework in controller of my project
var userContext =context.UserExtraDetails.Where(a => (DateTime.Now - a.LastEmailSent.Value).TotalDays >= 6)
.Select(a => new { Id = a.AspNetUser.Id,
email = a.AspNetUser.Email,
Fullname = a.AspNetUser.FirstName + " " + a.AspNetUser.LastName
}).ToList();
I am getting an error "DbArithmeticExpression arguments must have a numeric common type"
Values are not null, but not sure what is the issue.
Arithmetic with DateTime is not supported in Entity Framework, means EF doesn't support DateTime - DateTime like operations.
You cannot use subtraction in a query : the operator - is overloaded in .NET, but not in the SQL Server.
you can SqlFunctions.DateDiff
for your code so it will be like
var Usernotsent = context.UserExtraDetails
.Where(a => System.Data.Entity.SqlServer.SqlFunctions.DateDiff("day", a.LastEmailSent.Value, DateTime.Now) > 6)
.Select(a => new {
Id = a.AspNetUser.Id,
email = a.AspNetUser.Email,
Fullname = a.AspNetUser.FirstName + " " + a.AspNetUser.LastName,
EmailSub = a.EmailSubscribe,
lastsent = a.LastEmailSent })
.ToList();
When getting date differernce Standard LINQ request will fail with the statement like the following
var r = from e in Entity where e.Date.Subtract(DateTime.Today) <= 5;
and may throw above error "DbArithmeticExpression arguments must have a numeric common type", so if you using Entity framework 4 or above you can use EntityFunctions
var r = from e in Entity where EntityFunctions.DiffDays(DateTime.Today, e.Date) <= 5;
If you don't want to use EntityFunction @Vikas's above answer should help.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly