zoukankan      html  css  js  c++  java
  • dapper 父子集合查询 获取带有子集集合的数据

    Dapper之QueryAsync

    public static Task<IEnumerable> QueryAsync<TFirst, TSecond, TReturn>()
    应用场景:获取带有子集集合的数据
    • Parent——带有子节点的Model
    public class Parent
        {
            public Parent()
            {
                Child= new List<Child>();
            }
    
            public string Id { get; set; }
    
            public string Name { get; set; }
    
            public List<Child> Child{ get; set; }
        }
    

    Child——子节点model

    public class Child
        {
            public string Id { get; set; }
    
            public string Name { get; set; }
        }
    
     var lookup = new Dictionary<string, Parent>();
     var sql = " ";
     await db.QueryAsync<Parent, Child, Parent>(
                    sql,
                    (b, s) =>
                    {
                    	//获取父节点
                        Parenttmp;
                        if (!lookup.TryGetValue(b.Id, out tmp))
                        {
                            tmp = b;
                            lookup.Add(b.Id, tmp);
                        }
    					//获取子节点集合
                        if (s != null && (!tmp.Child.Any(x => x.Id == s.Id)))
                        {
                            tmp.Child.Add(s);
                        }
                        return b;
                    },
                    new { ParentId = id });
                 return lookup.Values.ToList();
  • 相关阅读:
    Oracle 函数
    Oracle select into from 和 insert into select
    SQL 子查询
    Java ThreadLocal 学习
    Structs 2
    Spring知识点
    java循环HashMap两种方法的效率比较
    Spring 面试复习
    Hibernate 知识点复习
    java 面试 复习 II
  • 原文地址:https://www.cnblogs.com/SpiritWalker/p/13978819.html
Copyright © 2011-2022 走看看