DbArithmeticExpression arguments must have a numeric common type
Arithmetic with DateTime
is not supported in Entity Framework 6 and earlier. You have to use DbFunctions*. So, for the first part of your statement, something like:
var sleeps = context.Sleeps(o =>
DbFunctions.DiffHours(o.ClientDateTimeStamp, clientDateTime) < 24);
Note that the DiffHours
method accepts Nullable<DateTime>
.
Entity Framwork core (when used with Sql Server, maybe other db providers) supports the DateTime AddXxx
functions (like AddHours
). They're translated into DATEADD
in SQL.
*EntityFunctions
prior to Entity Framework version 6.
DbArithmeticExpression error in linq request
I am not sure whether the ((TimeSpan)(DateTime.Now - d.createDate)).Minutes <= 30)
expression can be correctly converted to a storage query.
I would use the DbFunctions.DiffMinutes
method
verif = context.DeviceVerifications.Where(d => d.phone == phone
&& d.securityKey == key
&& d.validated == 0
&& DbFunctions.DiffMinutes(DateTime.Now, d.createDate) <= 30)).FirstOrDefault();
DbArithmeticExpression arguments must have a numeric common type. Entity framework
The problem seems to be the computation of XTimeSpent as you need to use sqlfunctions to subtract dates
DbArithmeticExpression arguments must have a numeric common type. in c# MVC
Just do this :
ViewBag.OnlineATE = db.UPTO_CarReceiption.Where(u => EntityFunctions.DiffMinutes(u.RegisterDate,DateTime.Now)<30).Select(i => i.TechnicalReviewCenterId).Distinct().Count();
LINQ DB Arithmetic must have common type (Date Comparison
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));
How to subtract two dates in LINQ?
Ultimately I use List and foreach.
Downloading data from database:
public int DateCleanFloor1to2(DateTime dateTime)
{
var dateList = (from Rezerwacje in db.Rezerwacje
join rooms in db.rooms on Rezerwacje.PokojID equals rooms.id
where (rooms.floor > 0 && rooms.floor <= 2) && (Rezerwacje.DataDo == dateTime)
select Rezerwacje).ToList();
return SubtractDateToDateFrom(dateList, dateTime.AddDays(25));
}
And method with foreach loop:
public int SubtractDateToDateFrom(List<Rezerwacje> dateList, DateTime dateTime)
{
int count = 0;
foreach (var item in dateList)
{
if (item.DataDo < dateTime)
{
count++;
}
}
return count;
}
@DevilSuichiro @Arulkumar @Seven
C# - LINQ to Entity query attaching the value of the properties to one variable
Sound like:
var sole = SoleService.All().Where(c => c.Status != 250)
.AsEnumerable()
.Select(x => new {
ID = x.ID,
Code = x.Code + x.Country,
Name = x.Name
})
.Distinct()
.OrderBy(s => s.Code);
You don't need inner anonymous type at all. If you are working on EF, sine string +
is not supported, call AsEnumerable
before doing select.
How to substract dates to timspan using Linq to EF?
If you want to recieve a TimeSpan value, you should simple subtract the two dates, without using any additional methods, like this:
var customers = from s in _db.Customers
select new
{
s.Id,
s.Name,
s.Address,
s.JoinDate,
s.ExpireDate,
Period = s.ExpireDate - s.JoinDate
};
Compare DateTime in Sqlite with Entity and Linq
Found a way - this works:
DateTime offsetHour = DateTime.Now.Add(new TimeSpan(-1, 0, 0));
backupEvents = backupEvents.Where(e => e.Timestamp >= offsetHour);
Related Topics
How to Force C# .Net App to Run Only One Instance in Windows
How to Download a Nuget Package Without Nuget.Exe or Visual Studio Extension
Ftpwebrequest Ftp Download with Progressbar
How to Deserialize Xml with Dynamic Element Names
Most Efficient Method of Self Referencing Tree Using Entity Framework
Could Not Load File or Assembly 'System.Web.Http 4.0.0 After Update from 2012 to 2013
Should I Take Ilogger, Ilogger<T>, Iloggerfactory or Iloggerprovider for a Library
How to Create a Button That Can Send Keys to a Conrol Without Losing Focus - Virtual Keyboard
Xml Deserialization of Collection Property with Code Defaults
Event Action<> VS Event Eventhandler<>
Custom Authentication in ASP.NET-Core
Encoding Space Character in Xml Name
In-Use Files Not Updated by Msi-Installer (Visual Studio Installer Project)
Should We Always Include a Default Constructor in the Class
A Textbox/Richtextbox That Has Syntax Highlighting? [C#]
Why Do Members of a Static Class Need to Be Declared as Static? Why Isn't It Just Implicit