简体   繁体   English

Linq2db:通过预先加载连接表

[英]Linq2db: join tables with eager loading

I'm using linq2db ORM in my project and have the following database schema (simplified):我在我的项目中使用 linq2db ORM 并具有以下数据库模式(简化):

在此处输入图像描述

I have the following method to get information about the user:我有以下方法来获取有关用户的信息:

public async Task<User> GetUser(int id)
{
  var user =
    await (
        from u in _db.Users
                     .LoadWith(u => u.Accounts)
                     .ThenLoad(a => a.Transactions)
        where u.Id == id
        from lang in _db.Languages.LeftJoin(l => l.Id == u.LanguageId)
        select u)
      .FirstOrDefaultAsync();

  return user;
}

However, I wanted to get the information about all the limits and aggregators for each account.但是,我想获取有关每个帐户的所有限制和聚合器的信息。 I believe there should be an efficient way to do that.我相信应该有一种有效的方法来做到这一点。 Could someone help me with that?有人可以帮我吗?

When we use LoadWith() , after using ThenLoad() , it's going to start from the root ( User table), like this:当我们使用LoadWith()时,使用ThenLoad()之后,它会从根( User表)开始,像这样:

from u in _db.Users
                 .LoadWith(u => u.Accounts)
                    .ThenLoad(a => a.Transactions)
                 .LoadWith(u => u.Accounts)
                    .ThenLoad(a => a.Limits)
                 .LoadWith(u => u.Accounts)
                    .ThenLoad(a => a.Aggregators);

You can also use Include() and ThenInclude() instead of LoadWith() and ThenLoad() .您还可以使用Include()ThenInclude()而不是LoadWith()ThenLoad() Include vs Load . 包含与加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM