将 Dapper 查询映射到对象集合(对象集合本身有几个集合)

本文关键字:集合 对象 有几个 Dapper 映射 查询 | 更新日期: 2023-09-27 18:33:48

我想执行单个查询(或具有多个结果集的存储过程)。 我知道如何使用 Dapper 进行多重映射,但我无法对如何将两个集合映射到同一个父级进行排序。 基本上,给定此对象定义...

class ParentObject
{
    string Name { get; set; }
    ICollection<ChildObjectOne> ChildSetOne {get;set;}
    ICollection<ChildObjectTwo> ChildSetTwo { get; set; }
}
class ChildObjectOne
{
    string Name { get; set; }
}
class ChildObjectTwo
{
    int id { get; set; }
    string LocationName { get; set; }
}

我希望能够运行一个以某种方式产生以下内容的 Dapper 查询:

IQueryable<ParentObject> result = cnn.Query(
          // Some really awesome dapper syntax goes here
);

将 Dapper 查询映射到对象集合(对象集合本身有几个集合)

不确定您是否不想使用多重映射,但以下是它如何适用于您的情况。据我所知和在SO上阅读,不可能用简单的Query映射深度嵌套的对象图。

 static void Main(string[] args)
        {
            var sqlParent = "SELECT parentId as Id FROM ParentTable WHERE parentId=1;";
            var sqlChildOneSet = "SELECT Name FROM ChildOneTable;"; // Add an appropriate WHERE
            var sqlChildTwoSet = "SELECT Id, LocationName FROM ChildTwoTable;"; // Add an appropriate WHERE
            var conn = GetConnection() // whatever you're getting connections with
            using (conn)
            {
                conn.Open();
                using (var multi = conn.QueryMultiple(sqlParent + sqlChildOneSet + sqlChildTwoSet))
                {
                    var parent = multi.Read<ParentObject>().First();
                    parent.ChildSetOne = multi.Read<ChildOne>().ToList();
                    parent.ChildSetTwo = multi.Read<ChildTwo>().ToList();
                }
            }
        }

嵌套对象和 dapper 的类似问题:

https://stackoverflow.com/search?q=nested+objects+%2B+dapper

在这种情况下,可以使用 IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TReturn> map); 方法具体化具有一对多关系的对象。但是,您需要对实体进行一些更改,以便有足够的信息来执行此操作。

以下是一些具有类似问题的SO线程。

如何使用 Dapper 映射嵌套对象的列表

扩展功能使其更清洁

Dapper.Net 示例 - 映射关系

public class ParentObject
{
    public ParentObject()
    {
        ChildSetOne = new List<ChildObjectOne>();
        ChildSetTwo = new List<ChildObjectTwo>();
    }
    // 1) Although its possible to do this without this Id property, For sanity it is advisable.
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<ChildObjectOne> ChildSetOne {get; private set;}
    public ICollection<ChildObjectTwo> ChildSetTwo { get; private set; }
}
public class ChildObjectOne
{
    // 2a) Need a ParentId
    public int ParentId { get; set; }
    public string Name { get; set; }
}
public class ChildObjectTwo
{
    // 2b) This ParentId is not required but again for sanity it is advisable to include it.
    public int ParentId { get; set; }
    public int id { get; set; }
    public string LocationName { get; set; }
}
public class Repository
{
    public IEnumerable<ParentObject> Get()
    {
        string sql = 
            @"SELECT 
                p.Id, 
                p.Name, 
                o.Name, 
                o.ParentId, 
                t.Id, 
                t.LocationName, 
                t.ParentId 
            FROM 
                Parent p 
                    LEFT JOIN ChildOne o on o.ParentId = p.Id 
                    LEFT JOIN ChildTwo t on t.ParentId = p.Id 
            WHERE 
                p.Name LIKE '%Something%'";
        var lookup = new Dictionary<int, ParentObject>();
        using (var connection = CreateConnection())
        {
            connection.Query<ParentObject, ChildObjectOne, ChildObjectTwo, ParentObject>(
                sql, (parent, childOne, childTwo) =>
                {
                    ParentObject activeParent;
                    if (!lookup.TryGetValue(childOne.ParentId, out activeParent)) 
                    {
                        activeParent = parent;
                        lookup.add(activeParent.Id, activeParent);
                    }
                    //TODO: if you need to check for duplicates or null do so here
                    activeParent.ChildSetOne.Add(childOne);
                    //TODO: if you need to check for duplicates or null do so here
                    activeParent.ChildSetTwo.Add(childTwo);
                });   
        }
        return lookup.Values;
    }
}