我偶然发现了这个问题,我使用了lambda连接的子集。鉴于我可以使用任何LINQ扩展,我应该如何实现以下连接:
为简单起见,表格定义为
CREATE TABLE [dbo].[TableA] (
[Key] INT IDENTITY (1, 1) NOT NULL,
[Value] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED ([Key] ASC)
);
CREATE TABLE [dbo].[TableB] (
[Key] INT IDENTITY (1, 1) NOT NULL,
[Value] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED ([Key] ASC)
);
或者如果您更喜欢代码
public class TableContext : DbContext
{
public DbSet<B> TableB { get; set; }
public DbSet<A> TableA { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConnectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TableB>().Property(o => o.Key).UseSqlServerIdentityColumn();
modelBuilder.Entity<TableA>().Property(o => o.Key).UseSqlServerIdentityColumn();
}
}
public class B : IKeyValue
{
public int Key { get; set; }
public string Value { get; set; }
}
public class A : IKeyValue
{
public int Key { get; set; }
public string Value { get; set; }
}
public interface IKeyValue
{
int Key { get; set; }
string Value { get; set; }
}
((A intersect not B) union (A intersect B))
var leftOuterJoin = TableA
.GroupJoin(
TableB,
a => a.Key,
b => b.Key,
(x, y) => new { TableA = x, TableA = y })
.SelectMany(
x => x.TableB.DefaultIfEmpty(),
(x, y) => new { TableA = x.TableA, TableB = y});
var innerJoin = TableA
.Join(
TableB,
a => a.Key,
b => b.Key,
(x, y) => x)
var fullOuterJoin = TableA
.FullOuterJoin(
TableB,
a => a.Key,
b => b.Key,
(x, y, Key) => new {x, y})
答案 0 :(得分:7)
对您而言,最重要的是知道如何执行INNER JOIN
和OUTER JOIN
。
对于 INNER JOIN ,您可以使用LINQ中的JOIN
,如下所示:
var result =
TableA
.Join(TableB, left => left.Id, right => right.ForeignKeyToTableA,
(left, right) => new { TableAColumns = left, TableBColumns = right });
您已在示例中显示的 OUTER JOIN 。
现在你需要混合你所知道的,以获得理想的结果。
例如,执行 FULL OUTER JOIN 在LINQ中执行类似此伪代码的操作:
SELECT TableA.*, TableB.* FROM TableA LEFT OUTER JOIN TableB
UNION
SELECT TableA.*, TableB.* FROM TableB LEFT OUTER JOIN TableA
这将在LINQ中如下:
var fullOuterJoin =
(
TableA
.GroupJoin(TableB,
left => left.Id, right => right.ForeignKeyId,
(left, right) => new { TableA = left, TableB = right })
.SelectMany(p => p.TableB.DefaultIfEmpty(), (x, y) =>
new { TableA = x.TableA, TableB = y })
)
.Union
(
TableB
.GroupJoin(TableA,
left => left.Id, right => right.ForeignKeyId,
(left, right) => new { TableA = right, TableB = left })
.SelectMany(p => p.TableA.DefaultIfEmpty(), (x, y) =>
new { TableA = y, TableB = x.TableB })
);
您图像的最后一个例子是:
var fullOuterJoinOnlyWithNulls =
fullOuterJoin
.Where(p => p.TableA == null || p.TableB == null);
RIGHT OUTER JOIN 只不过是 LEFT OUTER JOIN ,您可以像这样交换结果列:
var rightOuterJoin =
(
TableB
.GroupJoin(TableA,
left => left.Id, right => right.ForeignKeyId,
(left, right) => new { TableA = right, TableB = left })
.SelectMany(p => p.TableA.DefaultIfEmpty(), (x, y) =>
new { TableA = y, TableB = x.TableB })
);
像这样你可以构建所有的示例场景。只需在需要时检查 null 表。