date = date.Date;
var query = $@" SELECT t.Name AS 'Department or Division',c.RegistrationNumber AS 'Vehicle Registration Number'
,v.Model + ' ' + v.Variant AS 'Model/ Vehicle Description',c.ChasisNumber AS 'VIN/ Chassis Number'
,c.FirstRegistrationDate AS 'First Registration Date',Od.Reading AS 'Odometer Reading'
,Od.ReadingDate AS 'Odometer Reading Date',Vc.Description as 'Vehicle Category'
FROM clientproductvehicles c JOIN Clients t
ON c.TargetClientId=t.ClientId JOIN Vehicles v
ON c.vehicleid=v.vehicleid JOIN VehicleCategories Vc
ON C.VehicleCategoryId=Vc.VehicleCategoryId LEFT JOIN OdometerReadings Od
ON C.Clientproductvehicleid=Od.ClientProductVehicleId
WHERE c.ClientId={clientId}
AND c.ProductId={productId}
AND (c.LastOdometerReading BETWEEN 14000 AND 20000 or c.LastOdometerReading BETWEEN 29000 AND 35000)
AND (convert(varchar(10), Od.ReadingDate,103) = convert(varchar(10), {date},103))
AND ( C.VehicleCategoryId = 1)
AND c.ClientProductVehicleId
NOT IN
(SELECT I.ClientProductVehicleId
FROM Incidents i
JOIN IncidentStates iSt
ON i.IncidentId = iSt.IncidentId
WHERE (i.CategoryId = 4 or i.Description LIKE '%km%')
AND IncidentStatusId = 1)
ORDER BY 'Odometer Reading Date' DESC";
List<ClientProductVehicle> vehicleServiceDue = _moiFleetContext.Db.SqlQuery<ClientProductVehicle>(query).ToList();
我正在尝试获取所有车辆的列表,这些车辆的里程表读数介于Incidents
之内。
我一直收到这个错误,我无法看到我出错的地方,因为当我在SQLServer上运行查询时,我得到了我想要的结果。
答案 0 :(得分:4)
此部分最有可能出现错误:convert(varchar(10), {date},103))
您使用字符串插值在此处放入.NET DateTime
对象。问题是:您必须将其作为文本传递(使用正确的引号)并使用正确的数据库日期/时间格式(而不是本地计算机)。
通过使用参数可以更轻松地解决此问题。在您的查询中,使用@date
代替{date}
并传入EF查询调用中的参数。
答案 1 :(得分:-1)
您的查询可能有多个问题。一个问题是:
mapStateToProps
SQL Server不允许您按常量排序。是的,这是一个常量,而不是对列的引用。
您应该只对字符串和日期常量使用单引号。使用方括号来转义列名:
ORDER BY 'Odometer Reading Date'
(您也应该在ORDER BY [Odometer Reading Date]
中执行此操作。)
或者,更好的是,给出不需要转义的名称:
SELECT