如果我直接在 SQL 数据库上运行以下查询,它可以工作
var result =
from log in db.DeviceLogs
let byId =
db.DeviceLogs.Where(item => item.UserId == log.UserId)
let first =
byId.First(item => item.LogDate == byId.Min(min => min.LogDate))
where
log.Equals(first) || (log.LogDate - first.LogDate).Minutes > 5
select log;
但是,如果我针对过滤列表运行它会失败OleDb Exception
var filteredDeviceLogs = db.DeviceLogs
.Where(s => s.LogDate.Value.Date >= StartDate.Date
&& s.LogDate.Value.Date < EndDate.AddDays(1));
var result =
from log in filteredDeviceLogs
let byId =
db.DeviceLogs.Where(item => item.UserId == log.UserId)
let first =
byId.First(item => item.LogDate == byId.Min(min => min.LogDate))
where
log.Equals(first) || (log.LogDate - first.LogDate).Minutes > 5
select log;
bool status = result.Any(); //OleDb Unhandled Exception
异常详情:
Unrecognized keyword WHEN.
堆栈跟踪
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OleDb.OleDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
我认为这可能是转换中使用 CASE WHEN STATEMENT 的原因,但是如果我复制查询,则没有 CASE WHEN 语句
Linq 查询
SELECT [t0].[DeviceLogId], [t0].[DownloadDate], [t0].[DeviceId], [t0].[UserId], [t0].[LogDate], [t0].[Direction], [t0].[AttDirection], [t0].[C1], [t0].[C2], [t0].[C3], [t0].[C4], [t0].[C5], [t0].[C6], [t0].[C7], [t0].[WorkCode]
FROM [DeviceLogs] AS [t0]
WHERE ((EXISTS(
SELECT NULL AS [EMPTY]
FROM (
SELECT TOP (1) [t1].[DeviceLogId]
FROM [DeviceLogs] AS [t1]
WHERE ([t1].[LogDate] = ((
SELECT MIN([t2].[LogDate])
FROM [DeviceLogs] AS [t2]
WHERE [t2].[UserId] = [t0].[UserId]
))) AND ([t1].[UserId] = [t0].[UserId])
) AS [t3]
WHERE [t0].[DeviceLogId] = [t3].[DeviceLogId]
)) OR ((CONVERT(Int,(CONVERT(BigInt,(CONVERT(BigInt,(((CONVERT(BigInt,DATEDIFF(DAY, (
SELECT [t12].[LogDate]
FROM (
SELECT TOP (1) [t10].[LogDate]
FROM [DeviceLogs] AS [t10]
WHERE ([t10].[LogDate] = ((
SELECT MIN([t11].[LogDate])
FROM [DeviceLogs] AS [t11]
WHERE [t11].[UserId] = [t0].[UserId]
))) AND ([t10].[UserId] = [t0].[UserId])
) AS [t12]
), [t0].[LogDate]))) * 86400000) + DATEDIFF(MILLISECOND, DATEADD(DAY, DATEDIFF(DAY, (
SELECT [t12].[LogDate]
FROM (
SELECT TOP (1) [t10].[LogDate]
FROM [DeviceLogs] AS [t10]
WHERE ([t10].[LogDate] = ((
SELECT MIN([t11].[LogDate])
FROM [DeviceLogs] AS [t11]
WHERE [t11].[UserId] = [t0].[UserId]
))) AND ([t10].[UserId] = [t0].[UserId])
) AS [t12]
), [t0].[LogDate]), (
SELECT [t12].[LogDate]
FROM (
SELECT TOP (1) [t10].[LogDate]
FROM [DeviceLogs] AS [t10]
WHERE ([t10].[LogDate] = ((
SELECT MIN([t11].[LogDate])
FROM [DeviceLogs] AS [t11]
WHERE [t11].[UserId] = [t0].[UserId]
))) AND ([t10].[UserId] = [t0].[UserId])
) AS [t12]
)), [t0].[LogDate])) * 10000)) / 600000000)) % 60)) > @p0)) AND (CONVERT(DATE, [t0].[LogDate]) >= @p1) AND (CONVERT(DATE, [t0].[LogDate]) < @p2)