Linq 左外连接通过 Linq2db 排除
Linq left outer join with exclusion via Linq2db
使用 Linq2db
和 Ms Access
我想 select 所有没有目标的扇区因此我想执行带排除的左外连接:
Dim q10 = From s In db.Sectors
From t In db.Targets.Where(Function(f) f.id_sector = s.Id).DefaultIfEmpty
Where t Is Nothing
Select s
Linq2db
解析为:
-- Access
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id])
WHERE
[t1].* IS NULL <=========== HERE
这显然是错误的。
我也试过:
Dim q10 = From s In db.Sectors
From t In db.Targets.Where(Function(f) f.id_sector = s.Id And f Is Nothing).DefaultIfEmpty
Select s
接收:
-- Access
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id] AND [t1].* IS NULL) <=========== HERE
总结一下,我需要:
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id])
WHERE
[t1].[id_sector] IS NULL
条件Where t1.id_sector Is Nothing
怎么写(id_sector是FK
所以是Integer
所以不能是Nothing
.
我找到了答案。这是一个小的解决方法,但它有效。
因为此查询在 linq2db
中不起作用:
Dim q10 = From s In db.Sectors
From t In db.Targets.Where(Function(f) f.id_sector = s.Id).DefaultIfEmpty
Where t Is Nothing
Select s
我们可以这样写:
Dim q10 = From s In db.Sectors
Where Not (From t In db.Targets Select t.id_sector).Contains(s.Id)
Select s
但是生成的 SQL 看起来像这样:
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id])
WHERE
[t1].[id_sector] IS NULL
看起来像这样:
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
WHERE
NOT (EXISTS(
SELECT
*
FROM
[tblTarget] [t1]
WHERE
[t1].[id_sector] = [t2].[Id]
))
使用 Linq2db
和 Ms Access
我想 select 所有没有目标的扇区因此我想执行带排除的左外连接:
Dim q10 = From s In db.Sectors
From t In db.Targets.Where(Function(f) f.id_sector = s.Id).DefaultIfEmpty
Where t Is Nothing
Select s
Linq2db
解析为:
-- Access
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id])
WHERE
[t1].* IS NULL <=========== HERE
这显然是错误的。
我也试过:
Dim q10 = From s In db.Sectors
From t In db.Targets.Where(Function(f) f.id_sector = s.Id And f Is Nothing).DefaultIfEmpty
Select s
接收:
-- Access
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id] AND [t1].* IS NULL) <=========== HERE
总结一下,我需要:
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id])
WHERE
[t1].[id_sector] IS NULL
条件Where t1.id_sector Is Nothing
怎么写(id_sector是FK
所以是Integer
所以不能是Nothing
.
我找到了答案。这是一个小的解决方法,但它有效。
因为此查询在 linq2db
中不起作用:
Dim q10 = From s In db.Sectors
From t In db.Targets.Where(Function(f) f.id_sector = s.Id).DefaultIfEmpty
Where t Is Nothing
Select s
我们可以这样写:
Dim q10 = From s In db.Sectors
Where Not (From t In db.Targets Select t.id_sector).Contains(s.Id)
Select s
但是生成的 SQL 看起来像这样:
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
LEFT JOIN [tblTarget] [t1] ON ([t1].[id_sector] = [t2].[Id])
WHERE
[t1].[id_sector] IS NULL
看起来像这样:
SELECT
[t2].[Id],
[t2].[Name]
FROM
[tblSector] [t2]
WHERE
NOT (EXISTS(
SELECT
*
FROM
[tblTarget] [t1]
WHERE
[t1].[id_sector] = [t2].[Id]
))