gpt4 book ai didi

c# - 查询 List 时出现 OleDb 未处理的异常

转载 作者:行者123 更新时间:2023-11-30 18:33:49 24 4
gpt4 key购买 nike

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

异常详情:无法识别的关键字 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 语句的原因,但如果我复制查询,则没有 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)

最佳答案

我的猜测是,无论您通过 OLEDB 访问哪个数据源,都不支持 case 语句。

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

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

关于c# - 查询 List<T> 时出现 OleDb 未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16852580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com