从 SQL 查询中排除子类型
我有下面的 Linq 查询:
var result = (from booking in _context.Booking.AsNoTracking()
join vendor in _context.Vendor.AsNoTracking() on booking.VendorId equals vendor.Id
where
booking.ClientId == userId
orderby booking.Id descending
select vendor.TypeId).FirstOrDefault();
供应商可以分为移动设备或车库。 但对于我的用例,我不关心子类型。
生成的 SQL 包含带有 UNION 的子类型表。
exec sp_executesql N'SELECT TOP (1)
[Project3].[TypeId] AS [TypeId]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Join1].[TypeId] AS [TypeId]
FROM [dbo].[Booking] AS [Extent1]
INNER JOIN (SELECT [UnionAll1].[VendorId] AS [VendorId], [Extent4].[TypeId] AS [TypeId]
FROM (SELECT
[Extent2].[VendorId] AS [VendorId]
FROM [dbo].[Garage] AS [Extent2]
UNION ALL
SELECT
[Extent3].[VendorId] AS [VendorId]
FROM [dbo].[Mobile] AS [Extent3]) AS [UnionAll1]
INNER JOIN [dbo].[Vendor] AS [Extent4] ON [UnionAll1].[VendorId] = [Extent4].[Id] ) AS [Join1] ON [Extent1].[VendorId] = [Join1].[VendorId]
WHERE [Extent1].[ClientId] = @p__linq__0
) AS [Project3]
ORDER BY [Project3].[Id] DESC',N'@p__linq__0 uniqueidentifier',@p__linq__0='6AE9C275-7944-47B0-9B03-7B10EC88C98C'
这是浪费。 如何排除子类型不包含在 SQL 命令中?
奖金问题。 我可以让 TOP 1 在嵌套 SQL 中运行吗? 再次。这是浪费。它首先获得完整结果,然后获得 TOP 1。
I have the Linq query below:
var result = (from booking in _context.Booking.AsNoTracking()
join vendor in _context.Vendor.AsNoTracking() on booking.VendorId equals vendor.Id
where
booking.ClientId == userId
orderby booking.Id descending
select vendor.TypeId).FirstOrDefault();
A Vendor can be subtyped into a Mobile or a Garage.
But for my use case, I don't care about the subtype.
The SQL that is generated includes the subtype tables with a UNION.
exec sp_executesql N'SELECT TOP (1)
[Project3].[TypeId] AS [TypeId]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Join1].[TypeId] AS [TypeId]
FROM [dbo].[Booking] AS [Extent1]
INNER JOIN (SELECT [UnionAll1].[VendorId] AS [VendorId], [Extent4].[TypeId] AS [TypeId]
FROM (SELECT
[Extent2].[VendorId] AS [VendorId]
FROM [dbo].[Garage] AS [Extent2]
UNION ALL
SELECT
[Extent3].[VendorId] AS [VendorId]
FROM [dbo].[Mobile] AS [Extent3]) AS [UnionAll1]
INNER JOIN [dbo].[Vendor] AS [Extent4] ON [UnionAll1].[VendorId] = [Extent4].[Id] ) AS [Join1] ON [Extent1].[VendorId] = [Join1].[VendorId]
WHERE [Extent1].[ClientId] = @p__linq__0
) AS [Project3]
ORDER BY [Project3].[Id] DESC',N'@p__linq__0 uniqueidentifier',@p__linq__0='6AE9C275-7944-47B0-9B03-7B10EC88C98C'
This is wasteful.
How can I exclude the SubTypes from being included in the SQL command?
Bonus question.
Can I make the TOP 1 run in the nested SQL?
Again. This is wasteful. It gets a full result first, and then TOP 1s it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论