DbArithmeticExpression arguments must have a numeric common type


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.

 


Asked by:- jaya
2
: 9069 At:- 5/30/2017 11:30:37 AM
C# Linq Entity Framework







2 Answers
profileImage Answered by:- vikas_jk

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();
2
At:- 5/30/2017 3:19:15 PM
great, I wasn't aware of that.thank you 0
By : jaya - at :- 5/31/2017 9:04:34 AM
thanks, I had similar issue and it worked for me also, another possible solution can be to move Arithmetic operations on DateTime outside the EF query or before executing it 0
By : manish - at :- 2/16/2018 1:13:46 PM


profileImage Answered by:- manish

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.

1
At:- 9/7/2018 1:11:03 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use