首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >EF 10 LeftJoin & RightJoin 的支持

EF 10 LeftJoin & RightJoin 的支持

作者头像
郑子铭
发布2025-04-27 13:26:23
发布2025-04-27 13:26:23
2170
举报

EF 10 LeftJoin & RightJoin 的支持

Intro

我们前面介绍 Linq LeftJoin/RightJoin 的时候提到过 EF Core 也会支持 LeftJoin/RightJoin,EF Core 在 preview 1 的时候支持了 LeftJoin 在 preview 2 中支持了 RightJoin,现在 preview 2 已经发布我们一起来看下吧

Sample

测试示例如下:

代码语言:javascript
复制
public static async Task EFLeftRightJoinSample()
{
    var services = new ServiceCollection();
    services.AddSqlite<TestDbContext>("Data Source=test.db", optionsAction: options =>
    {
        options.LogTo(Console.WriteLine);
    });
    awaitusingvar serviceProvider = services.BuildServiceProvider();

    usingvar scope = serviceProvider.CreateScope();
    var dbContext = scope.ServiceProvider.GetRequiredService<TestDbContext>();
    await dbContext.Database.EnsureDeletedAsync();
    await dbContext.Database.EnsureCreatedAsync();

    dbContext.Jobs.Add(new Job() { Id = 1, Name = "test" });
    dbContext.Employees.Add(new Employee() { Id = 1, JobId = 1, Name = "Alice" });
    dbContext.Employees.Add(new Employee() { Id = 2, JobId = 2, Name = "Jane" });
    await dbContext.SaveChangesAsync();

    var result = await dbContext.Employees.AsNoTracking()
        // ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
        .LeftJoin(dbContext.Jobs.AsNoTracking(), e => e.JobId, j => j.Id, (e, j) => new
        {
            e.Id,
            e.Name,
            e.JobId,
            JobName =  j == null ? "Unknown" : j.Name
        })
        .ToArrayAsync();
    foreach (var item in result)
    {
        Console.WriteLine(JsonSerializer.Serialize(item));
    }

    result = await dbContext.Jobs.AsNoTracking()
        // ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
        .RightJoin(dbContext.Employees.AsNoTracking(), j => j.Id, e => e.JobId, (j, e) => new
        {
            e.Id,
            e.Name,
            e.JobId,
            JobName =  j == null ? "Unknown" : j.Name
        })
        .ToArrayAsync();
    foreach (var item in result)
    {
        Console.WriteLine(JsonSerializer.Serialize(item));
    }
}

为了方便看测试结果我们将 Log 输出到 Console 里方便打印实际数据库的查询是否符合预期

LeftJoin

RightJoin

从上面执行的 SQL 可以看得出来我们的 LeftJoin & RightJoin 被正确翻译成了 SQL

大家留意代码的话会发现有一行注释

代码语言:javascript
复制
// ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall

这行注释是给 Resharper 和 Rider 的,不加的话目前会有一个 warning,示例如下:

在之前的版本里不能转成 SQL 但从 EF Core 10 开始就可以了,ReSharper 后续应该会有所改进避免对 LeftJoin/RightJoin 产生 warning 吧

References

  • https://github.com/dotnet/efcore/issues/35379
  • https://github.com/dotnet/efcore/issues/12793
  • https://github.com/dotnet/efcore/pull/35451
  • https://github.com/dotnet/efcore/issues/35367
  • https://github.com/dotnet/efcore/pull/35462
  • https://github.com/WeihanLi/SamplesInPractice/blob/main/net10sample/Net10Samples/LinqSamples.cs#L74
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DotNet NB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • EF 10 LeftJoin & RightJoin 的支持
    • Intro
    • Sample
    • References
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档