將Dapper查詢映射到對象集合(它本身有幾個集合)

dapper multi-mapping

我想執行單個查詢(或具有多個結果集的存儲過程)。我知道如何使用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
);

一般承認的答案

不確定你是否不想使用MultiMapping,但這裡是它如何適用於你的情況。據我所知並在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();
                }
            }
        }

嵌套對象和小巧的類似問題:

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);可以實現具有一對多關係的對象。 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;
    }
}


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因