3表LINQ查询带有where子句

时间:2011-07-22 04:22:36

标签: c# sql linq

这个简单而又邪恶的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 cause(这里列出的那个),当我尝试通过var查询结果进行foreach时,我收到一个'Unhandled Exception:System.ArgumentException:Value不在预期的范围内'错误。请注意,当我删除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)替换为:tDate tDate只是一个本地字符串= day.ToString(dtForm)

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

埃文:

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