LINQ 左外连接查询错误:OuterApply 没有适当的键
Posted
技术标签:
【中文标题】LINQ 左外连接查询错误:OuterApply 没有适当的键【英文标题】:LINQ left outer join query error: OuterApply did not have the appropriate keys 【发布时间】:2015-01-31 00:57:11 【问题描述】:我正在使用实体框架作为我的 ORM 对两个 SQL 函数进行连接。执行查询时,我收到以下错误消息:
The query attempted to call 'Outer Apply' over a nested query,
but 'OuterApply' did not have the appropriate keys
这是我的查询:
var ingredientAllergenData = (from ings in db.fnListIngredientsFromItem(productId, (short)itemType, productId)
join ingAllergens in db.fnListAllergensFromItems(productId.ToString(CultureInfo.InvariantCulture), (short)itemType, currentLang)
on ings.id equals ingAllergens.ingredientId into ingAllergensData
from allergens in ingAllergensData.DefaultIfEmpty()
where ings.table == "tblIng" || ings.table == ""
select new ings, allergens).ToList();
我在 LINQPad 中编写了相同的查询并得到了结果,所以我不确定问题是什么:
var ingredientAllergenData = (from ings in fnListIngredientsFromItem(1232, 0, 1232)
join ingAllergens in fnListAllergensFromItems("1232", 0, 1)
on ings.Id equals ingAllergens.IngredientId into ingAllergensData
from allergens in ingAllergensData.DefaultIfEmpty()
where ings.Table == "tblIng" || ings.Table == ""
select new ings, allergens).ToList();
来自 linqpad 的响应:
编辑 这是 LINQPad 中生成的 SQL 查询:
-- Region Parameters
DECLARE @p0 Int = 1232
DECLARE @p1 Int = 0
DECLARE @p2 Int = 1232
DECLARE @p3 VarChar(1000) = '1232'
DECLARE @p4 SmallInt = 0
DECLARE @p5 Int = 1
DECLARE @p6 VarChar(1000) = 'tblIng'
DECLARE @p7 VarChar(1000) = ''
-- EndRegion
SELECT [t0].[prodId] AS [ProdId], [t0].[id] AS [Id], [t0].[parent] AS [Parent], [t0].[name] AS [Name], [t0].[ing_gtin] AS [Ing_gtin], [t0].[ing_artsup] AS [Ing_artsup], [t0].[table] AS [Table], [t0].[quantity] AS [Quantity], [t2].[test], [t2].[prodId] AS [ProdId2], [t2].[ingredientId] AS [IngredientId], [t2].[allergenId] AS [AllergenId], [t2].[allergenName] AS [AllergenName], [t2].[level_of_containment] AS [Level_of_containment]
FROM [dbo].[fnListIngredientsFromItem](@p0, @p1, @p2) AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[prodId], [t1].[ingredientId], [t1].[allergenId], [t1].[allergenName], [t1].[level_of_containment]
FROM [dbo].[fnListAllergensFromItems](@p3, @p4, @p5) AS [t1]
) AS [t2] ON [t0].[id] = ([t2].[ingredientId])
WHERE ([t0].[table] = @p6) OR ([t0].[table] = @p7)
我还尝试将相同的数字硬编码到 C# 中,但再次遇到相同的错误。
【问题讨论】:
我会首先尝试从查询中进行所有转换(转换为short、.ToString 调用等),然后将它们分配给变量并在查询中使用这些转换。看起来 LINQ 可能会因为尝试将转换作为查询的一部分而窒息。 您应该在 Linqpad 中尝试完全相同的查询(使用变量)。 感谢您的建议,但这是我做的第一件事。 它抱怨 OUTER APPLY 的原因是它想为你的第一个函数返回的每一行执行你的第二个函数。如果不会有大量的行,您最好分别获取每个函数的结果,然后进行内存连接,而不是尝试在数据库级别进行。 @Aleks LINQ 正在尝试执行外部应用而不是连接。当您编写外部应用程序时,您通常会获取函数 1 返回的一些输出并将其用作函数 2 的输入。由于您传递的值不是来自函数 1 LINQ 抱怨它不知道如何编写外适用。因此,如果您仍然打算在单个数据库调用中执行此操作,则需要从第一个函数中找到一些可以馈送到第二个函数的输出。 ProductId 看起来可能是一个不错的候选者,但我不知道该函数的确切结果。 【参考方案1】:问题是实体框架需要知道 TVF 结果的主键列是什么来进行左连接,而默认生成的 EDMX 文件不包含该信息。您可以通过将 TVF 结果映射到实体来添加键值信息(而不是默认映射到复杂类型)。
相同查询在 LINQPad 中起作用的原因是用于连接到 LINQPad 中数据库的默认数据上下文驱动程序使用 LINQ to SQL(而不是实体框架)。但我能够让查询在实体框架中运行(最终)。
我在本地建立了一个类似表值函数的SQL Server数据库:
CREATE FUNCTION fnListIngredientsFromItem(@prodId int, @itemType1 smallint, @parent int)
RETURNS TABLE
AS
RETURN (
select prodId = 1232, id = 1827, parent = 1232, name = 'Ossenhaaspunten', ing_gtin = 3003210089821, ing_artsup=141020, [table] = 'tblIng', quantity = '2 K'
);
go
CREATE FUNCTION fnListAllergensFromItems(@prodIdString varchar(1000), @itemType2 smallint, @lang int)
RETURNS TABLE
AS
RETURN (
select prodId = '1232', ingredientId = 1827, allergenId = 11, allergenName = 'fish', level_of_containment = 2
union all
select prodId = '1232', ingredientId = 1827, allergenId = 16, allergenName = 'tree nuts', level_of_containment = 2
union all
select prodId = '1232', ingredientId = 1827, allergenId = 12, allergenName = 'crustacean and shellfish', level_of_containment = 2
);
go
我使用 Entity Framework 6.1.2 创建了一个测试项目,并使用 Visual Studio 2013 中的实体数据模型设计器从数据库生成了一个 EDMX 文件。通过此设置,我在尝试运行时遇到了同样的错误该查询:
System.NotSupportedException
HResult=-2146233067
Message=The query attempted to call 'OuterApply' over a nested query, but 'OuterApply' did not have the appropriate keys.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.ApplyOpJoinOp(Op op, Node n)
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.VisitApplyOp(ApplyBaseOp op, Node n)
at System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitorOfT`1.Visit(OuterApplyOp op, Node n)
...
为左连接运行备用表达式会导致稍有不同的错误:
var ingredientAllergenData = (db.fnListIngredientsFromItem(1323, (short)0, 1)
.GroupJoin(db.fnListAllergensFromItems("1232", 0, 1),
ing => ing.id,
allergen => allergen.ingredientId,
(ing, allergen) => new ing, allergen
)
).ToList();
这是新异常中截断的堆栈跟踪:
System.NotSupportedException
HResult=-2146233067
Message=The nested query does not have the appropriate keys.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.ConvertToSingleStreamNest(Node nestNode, Dictionary`2 varRefReplacementMap, VarList flattenedOutputVarList, SimpleColumnMap[]& parentKeyColumnMaps)
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.Visit(PhysicalProjectOp op, Node n)
at System.Data.Entity.Core.Query.InternalTrees.PhysicalProjectOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n)
...
Entity Framework 是开源的,所以我们实际上可以查看引发此异常的源代码。这个 sn-p 中的 cmets 解释了问题所在(https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Core/Query/PlanCompiler/NestPullup.cs):
// Make sure that the driving node has keys defined. Otherwise we're in
// trouble; we must be able to infer keys from the driving node.
var drivingNode = nestNode.Child0;
var drivingNodeKeys = Command.PullupKeys(drivingNode);
if (drivingNodeKeys.NoKeys)
// ALMINEEV: In this case we used to wrap drivingNode into a projection that would also project Edm.NewGuid() thus giving us a synthetic key.
// This solution did not work however due to a bug in SQL Server that allowed pulling non-deterministic functions above joins and applies, thus
// producing incorrect results. SQL Server bug was filed in "sqlbuvsts01\Sql Server" database as #725272.
// The only known path how we can get a keyless drivingNode is if
// - drivingNode is over a TVF call
// - TVF is declared as Collection(Row) is SSDL (the only form of TVF definitions at the moment)
// - TVF is not mapped to entities
// Note that if TVF is mapped to entities via function import mapping, and the user query is actually the call of the
// function import, we infer keys for the TVF from the c-space entity keys and their mappings.
throw new NotSupportedException(Strings.ADP_KeysRequiredForNesting);
这解释了导致该错误的路径,因此我们可以采取任何措施来摆脱该路径,从而解决问题。假设我们必须对表值函数的结果进行左连接,一个选项(也许是唯一的选项?)是将 TVF 的结果映射到具有主键的实体。然后 Entity Framework 将根据到该实体的映射知道 TVF 结果的键值,我们应该避免这些与缺少键有关的错误。
默认情况下,从数据库生成 EDMX 文件时,TVF 会映射到复杂类型。 https://msdn.microsoft.com/en-us/library/vstudio/ee534438%28v=vs.100%29.aspx 有关于如何更改它的说明。
在我的测试项目中,我添加了一个空表,其架构与 TVF 的输出相匹配,以让模型设计器生成实体,然后我转到模型浏览器并更新函数导入以返回这些集合实体(而不是自动生成的复杂类型)。进行这些更改后,相同的 LINQ 查询运行没有错误。
var ingredientAllergenData = (from ings in db.fnListIngredientsFromItem(productId, (short)itemType, productId)
join ingAllergens in db.fnListAllergensFromItems(productId.ToString(CultureInfo.InvariantCulture), (short)itemType, currentLang)
on ings.id equals ingAllergens.ingredientId into ingAllergensData
from allergens in ingAllergensData.DefaultIfEmpty()
where ings.table == "tblIng" || ings.table == ""
select new ings, allergens).ToList();
这是查询给我的跟踪 SQL:
SELECT
1 AS [C1],
[Extent1].[prodId] AS [prodId],
[Extent1].[id] AS [id],
[Extent1].[parent] AS [parent],
[Extent1].[name] AS [name],
[Extent1].[ing_gtin] AS [ing_gtin],
[Extent1].[ing_artsup] AS [ing_artsup],
[Extent1].[table] AS [table],
[Extent1].[quantity] AS [quantity],
[Extent2].[prodId] AS [prodId1],
[Extent2].[ingredientId] AS [ingredientId],
[Extent2].[allergenId] AS [allergenId],
[Extent2].[allergenName] AS [allergenName],
[Extent2].[level_of_containment] AS [level_of_containment]
FROM [dbo].[fnListIngredientsFromItem](@prodId, @itemType1, @parent) AS [Extent1]
LEFT OUTER JOIN [dbo].[fnListAllergensFromItems](@prodIdString, @itemType2, @lang) AS [Extent2] ON ([Extent1].[id] = [Extent2].[ingredientId]) OR (([Extent1].[id] IS NULL) AND ([Extent2].[ingredientId] IS NULL))
WHERE [Extent1].[table] IN ('tblIng','')
【讨论】:
“干得好,亲爱的华生!”我想知道摆脱异常的另一种方法是添加OrderBy
:db.fnListIngredientsFromItem(productId, (short)itemType, productId).OrderBy(x => x.ProdId)
。见this。
很好的答案,它给了我解决另一个问题的想法。
很好的解释!就我而言,我将 TVF 的结果分组,然后执行where g.Count() == numSearchTerms || g.FirstOrDefault().SearchTerm == null
。读到这里,我有了尝试where g.Count() == numSearchTerms || g.Any(x => x.SearchTerm == null)
的想法,它奏效了!
通过第一个查询 .ToList() 并且仅在 GroupBy 为 mi 工作后获得小结果以上是关于LINQ 左外连接查询错误:OuterApply 没有适当的键的主要内容,如果未能解决你的问题,请参考以下文章