使用dapper返回对象列表,每个父母包含两个其他对象

发布于 2025-02-11 13:59:11 字数 695 浏览 0 评论 0原文

我是Dapper的新手,并有一个3型模型类示例。 SQL数据库具有下面每个模型的关联表。

我的目的是填充作业对象列表(即作业表中的所有记录),每个记录都包含其关联状态&客户端对象。使用Dapper将如何实现?

感谢您提供的任何帮助!

SQL将是:

选择j。*,s。*,c。 >

模型是:

    public class StatusModel
    {
          public int Id { get; set; }
          public string StatusName { get; set; }
    }


   public class ClientModel
   {
      public int Id { get; set; }
      public string ClientName { get; set; }
      
   }


    public class JobModel
    {

      public int Id { get; }

      public string Description { get; set; }

      public StatusModel Status {get; set; }
      public ClientModel Client { get; set; }
    }

I'm new to Dapper, and have a 3 model class example. The SQL Database has associated tables for each model below.

My aim is to populate a list of Job objects (ie. all records in the Job table), each containing its associated Status & Client objects. How would this be achieved using Dapper?

Thank you kindly for any help you can give me!

The SQL will be :

SELECT j.*, s.*, c.* FROM Job j INNER JOIN Status s ON j.StatusId = s.Id INNER JOIN Client ON j.ClientId = c.Id;

The models are :

    public class StatusModel
    {
          public int Id { get; set; }
          public string StatusName { get; set; }
    }


   public class ClientModel
   {
      public int Id { get; set; }
      public string ClientName { get; set; }
      
   }


    public class JobModel
    {

      public int Id { get; }

      public string Description { get; set; }

      public StatusModel Status {get; set; }
      public ClientModel Client { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2025-02-18 13:59:11

您可以使用Dapper的Multimap功能。

using (SqlConnection connection = new SqlConnection(connectionString))
{
    var jobs = await connection.QueryAsync<JobModel, StatusModel, ClientModel, JobModel>(sql, (job, status, client) =>
    {
        job.Status = status;
        job.Client = client;
        return job;
    }, splitOn: "StatusID,ClientID");   
}

您可以阅读更多在这里

You can use Dapper's multimap feature.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    var jobs = await connection.QueryAsync<JobModel, StatusModel, ClientModel, JobModel>(sql, (job, status, client) =>
    {
        job.Status = status;
        job.Client = client;
        return job;
    }, splitOn: "StatusID,ClientID");   
}

You can read more here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文