gpt4 book ai didi

c# - 3 带有 where 子句的表 LINQ 查询

转载 作者:行者123 更新时间:2023-11-30 17:17:05 30 4
gpt4 key购买 nike

这个简单但邪恶的 LINQ 查询在运行时给我带来了问题(请注意,只要我不使用 JOIN,任何适当的 where 子句都可以工作:

var query = from iDay in db.DateTimeSlot
join tsk in db.Tasks on iDay.FkTask equals tsk.PkTask
join dte in db.Mdate on iDay.FkDate equals dte.PkDate
where dte.Mdate1 == day.ToString(dtForm)
select new {
tsk.PkTask,
tsk.Task,
iDay.FkTask,
iDay.TimeSlot,
iDay.Mdate,
dte.Mdate1
};

我可以让 where 子句在运行时工作,但前提是它应用于 db.DateTimeSlot 列。否则,如果我删除 where 子句,查询将起作用。如果我尝试使用正确的 where 原因,即此处列出的原因,当我尝试通过 var 查询结果进行 foreach 时,我会收到“未处理的异常:System.ArgumentException:值不在预期范围内”错误。请注意,当我删除 JOIN 子句时,where 子句确实有效,当我查询适当的表时。

数据库的模式是:

tasks -∞ dateTimeSlot ∞- mdate

我正在尝试获取与某个 mdate.date 相关的任务列表,因此 where 子句测试 mdate.date。

谢谢

编辑:这是这部分的 Sqlite 数据库模式:

CREATE TABLE mdate (
pkDate INTEGER PRIMARY KEY AUTOINCREMENT,
mdate TEXT,
nDay TEXT);
CREATE TABLE dateTimeSlot (
pkDTS INTEGER PRIMARY KEY AUTOINCREMENT,
timeSlot INTEGER,
fkDate INTEGER,
fkTask INTEGER,

FOREIGN KEY(fkDate) REFERENCES mdate(pkDate)
FOREIGN KEY(fkTask) REFERENCES tasks(pkTask));
CREATE TABLE mdate (
pkDate INTEGER PRIMARY KEY AUTOINCREMENT,
mdate TEXT,
nDay TEXT);

编辑:这是有效的 SQL 语句:

sqlite> SELECT tasks.task, mdate.mdate FROM dateTimeSlot
...> INNER JOIN tasks ON dateTimeSlot.fkTask=tasks.pkTask
...> INNER JOIN mdate ON dateTimeSlot.fkDate=mdate.pkDate
...> where mdate.mdate = '2011-07-21';
task|mdate
laundry|2011-07-21
laundry|2011-07-21

编辑:这是 Db.Log = Console.Out 的输出。请注意,如果保留 where 子句,我不会收到此 SQL 垃圾邮件,我只会收到正常的异常调试垃圾邮件:

SELECT tsk$.[pkTask], tsk$.[task], iDay$.[fkTask], iDay$.[timeSlot], t1$.[mdate], t1$.[nDay], t1$.[pkDate], dte$.[mdate]
FROM [main].[dateTimeSlot] AS iDay$
LEFT JOIN [main].[mdate] AS t1$ ON t1$.[pkDate] = iDay$.[fkDate]
INNER JOIN [main].[mdate] AS dte$ ON iDay$.[fkDate] = dte$.[pkDate]
INNER JOIN [main].[tasks] AS tsk$ ON iDay$.[fkTask] = tsk$.[pkTask]
-- Context: SqlServer Model: AttributedMetaModel Build: 4.0.0.0

我将完整的错误发布在:here

解决了!我将 day.ToString(dtForm) 替换为:tDatetDate 只是一个本地字符串 = day.ToString(dtForm)

最佳答案

您的异常输出突出显示了问题,因为您使用的 L2S 提供程序无法将 day.ToString(dtForm) 转换为 SQLite 可以理解的任何形式。

好处是这基本上是查询的固定字符串,不依赖于任何东西。您必须将其从查询中删除,但只需将其提升到局部变量即可:

var mdate1 = day.ToString(dtForm);
var query = from iDay in db.DateTimeSlot
join tsk in db.Tasks on iDay.FkTask equals tsk.PkTask
join dte in db.Mdate on iDay.FkDate equals dte.PkDate
where dte.Mdate1 == mdate1
select new
{
tsk.PkTask,
tsk.Task,
iDay.FkTask,
iDay.TimeSlot,
iDay.Mdate,
dte.Mdate1
};

异常的相关部分是指向这个方向的 AnalyzeToString 位:

Unhandled Exception: System.ArgumentException: Value does not fall within the expected range.
at DbLinq.Data.Linq.Sugar.Implementation.ExpressionDispatcher.AnalyzeToString (System.Reflection.MethodInfo method, IList`1 parameters, DbLinq.Data.Linq.Sugar.BuilderContext builderContext) [0x00151] in /var/tmp/portage/dev-lang/mono-2.10.2-r1/work/mono-2.10.2/mcs/class/System.Data.Linq/src/DbLinq/Data/Linq/Sugar/Implementation/ExpressionDispatcher.Analyzer.cs:466
at DbLinq.Data.Linq.Sugar.Implementation.ExpressionDispatcher.AnalyzeUnknownCall (System.Linq.Expressions.MethodCallExpression expression, IList`1 parameters, DbLinq.Data.Linq.Sugar.BuilderContext builderContext) [0x0008d] in /var/tmp/portage/dev-lang/mono-2.10.2-r1/work/mono-2.10.2/mcs/class/System.Data.Linq/src/DbLinq/Data/Linq/Sugar/Implementation/ExpressionDispatcher.Analyzer.cs:345
at DbLinq.Data.Linq.Sugar.Implementation.ExpressionDispatcher.AnalyzeCall (System.Linq.Expressions.MethodCallExpression expression, IList`1 parameters, DbLinq.Data.Linq.Sugar.BuilderContext builderContext) [0x00040] in /var/tmp/portage/dev-lang/mono-2.10.2-r1/work/mono-2.10.2/mcs/class/System.Data.Linq/src/DbLinq/Data/Linq/Sugar/Implementation/ExpressionDispatcher.Analyzer.cs:178

关于c# - 3 带有 where 子句的表 LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6785637/

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