Dapper之QueryAsync

tech2023-07-13  108

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(); PS:别人的代码,自己解读了一下,如有不对,欢迎━(`∀´)ノ亻!发言,指正(๑‾ ꇴ ‾๑)
最新回复(0)