带where子句的LINQ查询表

本文关键字:LINQ 查询表 子句 where | 更新日期: 2023-09-27 18:03:22

这个简单但邪恶的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子句在运行时工作,但只有当它适用于数据库。DateTimeSlot列。否则,如果删除where子句,查询将正常工作。如果我尝试使用此处列出的正确where原因,我将收到一个"未处理的异常:System"。ArgumentException:当我试图通过var查询结果foreach时,值不属于预期范围的错误。注意,当我去掉JOIN子句时,where子句在查询适当的表时确实起作用了。

数据库的模式为:

tasks -∞ dateTimeSlot ∞- mdate

我正试图获得与某个候选人相关的任务列表。日期,因此where子句测试mdate.date.

感谢

编辑:下面是这部分的Sqlite DB模式:

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

我把完整的错误贴在:这里

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

带where子句的LINQ查询表

您的异常输出突出了问题,因为您正在使用的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

Evan:

我想知道day.ToString(dtForm)的值是多少?是在1753年1月1日之前吗?如果是这样,SQL Server将不接受。