我有一个 Linq 查询的左外连接组:
var query =
from iDef in taskDb.TaskDefinitions
join iParam in taskDb.TaskParameters
on iDef.Id equals iParam.TaskId into NullableParameters
from iParam in NullableParameters.DefaultIfEmpty()
join iExecution in executions
on iDef.Id equals iExecution.TaskId into NullableExecutions
from iExecution in NullableExecutions.DefaultIfEmpty()
group iParam by new { Definition = iDef, Execution = iExecution } into g
select new QueryDomain
{
Definition = g.Key.Definition,
Parameters = g,
Execution = g.Key.Execution,
};
public class QueryDomain
{
public TaskDefinition Definition { get; set; }
public IEnumerable<TaskParameter> Parameters { get; set; }
public TaskExecution Execution { get; set; }
}
查询不断抛出
System.Data.SqlClient.SqlException (0x80131904): Invalid object name
'dbo.TaskParameter'.
我在执行前停下来检查底层的 SQL 语句,由于某种原因,它似乎没有从表 TaskParameter 中选择任何字段
SELECT [t4].*
FROM (
SELECT [t0].*, [t3].*
FROM [dbo].[TaskDefinition] AS [t0]
LEFT OUTER JOIN [dbo].[TaskParameter] AS [t1] ON [t0].[Id] = [t1].[TaskId]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t2].* FROM [dbo].[TaskExecution] AS [t2]
) AS [t3] ON ([t0].[Id] = [t3].[TaskId]) AND ([t3].[AsOf] = @p0)
GROUP BY [t0].*, [t3].*
) AS [t4]
WHERE [t4].[CsType] = @p1
我简化了查询以使其更易于阅读。
SQL 有效,但它没有从 TaskParameter 中选择任何值。
有人知道发生了什么吗?
最后,我有三张桌子。TaskDefinition 是主表。TaskExecution 可能包含 0 或 1 个条目索引 (FK) TaskDefinition。TaskParameter 可以包含任意数量 (>=0) 的条目索引 (FK) TaskDefinition。QueryDomain 反映了查询结果的所需结构。谢谢。