c# – 如何在lambda中进行sql连接?

前端之家收集整理的这篇文章主要介绍了c# – 如何在lambda中进行sql连接?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不时地偶然发现这个问题,我使用了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,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(),y) => new { TableA = x.TableA,TableB = y});

(A intersects B)

var innerJoin = TableA
  .Join(
    TableB,y) => x)

(A union B)

var fullOuterJoin = TableA
  .FullOuterJoin(
    TableB,y,Key) => new {x,y})

解决方法

对你来说最重要的是知道如何执行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,right => right.ForeignKeyId,right) => new { TableA = left,TableB = right })
                .SelectMany(p => p.TableB.DefaultIfEmpty(),y) => 
                    new { TableA = x.TableA,TableB = y })
            )
            .Union
            (
                TableB
                .GroupJoin(TableA,right) => new { TableA = right,TableB = left })
                .SelectMany(p => p.TableA.DefaultIfEmpty(),y) => 
                    new { TableA = y,TableB = x.TableB })
            );

然后,您图像的最后一个示例是:

var fullOuterJoinOnlyWithNulls =
            fullOuterJoin
            .Where(p => p.TableA == null || p.TableB == null);

一个正确的外部联接只是一个LEFT OUTER JOIN,您可以像这样交换结果列:

var rightOuterJoin =
            (
                TableB
                .GroupJoin(TableA,y) =>
                    new { TableA = y,TableB = x.TableB })
            );

像这样你可以构建所有的示例场景.只需在需要时检查表是否为null.

原文链接:https://www.f2er.com/csharp/99567.html

猜你在找的C#相关文章