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();
最新评论