gpt4 book ai didi

sql - 为什么EF查询引用属性时会生成这个sql

转载 作者:行者123 更新时间:2023-12-04 08:53:30 26 4
gpt4 key购买 nike

使用 AdventureWorks 数据库并发出此查询时:

   AdventureWorksEntities entities = new AdventureWorksEntities();
entities.Contacts.Where(x => x.FirstName == "A"
|| x.FirstName == "B"
|| x.FirstName == "C")
.ToList();

将被翻译成这个sql,这是它所能做到的最好的:
//ommited for brevity
FROM [Person].[Contact] AS [Extent1]
WHERE [Extent1].[FirstName] IN (N'A',N'B',N'C')

但是,当我发出此查询时:
entities.Employee.Where(x => x.Contact.FirstName == "A" 
|| x.Contact.FirstName == "B"
|| x.Contact.FirstName == "C")
.ToList();

我得到这个 SQL:
//ommited for brevity
FROM [HumanResources].[Employee] AS [Extent1]
INNER JOIN [Person].[Contact] AS [Extent2] ON [Extent1].[ContactID] = [Extent2].[ContactID]
LEFT OUTER JOIN [Person].[Contact] AS [Extent3] ON [Extent1].[ContactID] = [Extent3].[ContactID]
WHERE [Extent2].[FirstName] = N'A' OR [Extent3].[FirstName] IN (N'B',N'C')

为什么我会得到内部和外部联接,并且 EF 是否将它们拆分为 where ?

注意使用 contains 创建相同的 SQL:
var names = new List<string>{"A", "B", "C"};
entities.Employee.Where(x => names.Contains(x.Contact.FirstName)).ToList();

编辑:所以它似乎是一个 EF 错误,我已经接受了提供解决方法的答案

编辑:打开连接问题,它位于 here

最佳答案

编写一个接受 TVP 作为输入参数的存储过程,并让 EF 实现来自 SP 的结果:)

关于sql - 为什么EF查询引用属性时会生成这个sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4829286/

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