3

如果我直接在 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)
4

1 回答 1

0

我的猜测是,您通过 OLEDB 访问的任何数据源都不支持 case 语句。

Linq 查询被翻译成包含WHEN关键字(可能是CASE语句的一部分)的 SQL,但 OLEDB 可用于包装各种数据源,并非所有数据源都支持这种结构。

如果您正在访问 SQL Server、MySQL、Oracle 或其他主要数据库系统,那么您应该为该系统使用适当的 ADO.NET 提供程序而不是 OLEDB。如果您正在访问其他一些数据源,例如 Excel,那么您可能不走运。如果您可以找出正在向数据源发出什么 SQL,那么这可能会为解决此问题提供更多信息。

于 2013-05-31T08:09:47.127 回答