Dapper三表联查(自表连接)去除查询结果重复
我在使用Dapper进行三表联查时,出现了查询结果重复的情况,若只对纯结果Distinct(),则会B表添加C表时产生重复
public IEnumerable<Category> GetTCategories() { var sql = "select * from T_Category A inner join T_Category B on A.Id=B.pid Left join T_Category C on B.Id=C.pid where A.catelevel='一级'"; Category category = null; Category child = null; var user = _dapperhelper.Query<Category, Category, Category, Category>(sql, (u, ul,ull) => { if (category == null || category.Id != u.Id) { category = u; } if (child == null || child.Id != ul.Id) { child = ul; } if (ull != null) { child.childAuth.Add(ull); } if (ul != null) { category.childAuth.Add(child); } return category; }, null, null, true, splitOn:"Id", null, null); return user; }
表结构:

实体结构:
public class Category { public int Id { get; set; } public string catename { get; set; } public string catelevel { get; set; } public int pid { get; set; } public List<Category> childAuth { get; set; } = new List<Category>(); }
表数据:

查询结果:
"data": [ { "id": 9, "catename": "大家电", "catelevel": "一级", "pid": 0, "childAuth": [ { "id": 10, "catename": "电视", "catelevel": "二级", "pid": 9, "childAuth": [ { "id": 14, "catename": "海信", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 15, "catename": "夏普", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 16, "catename": "创维", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 17, "catename": "TCL", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 18, "catename": "小米", "catelevel": "三级", "pid": 10, "childAuth": [] } ] }, { "id": 10, "catename": "电视", "catelevel": "二级", "pid": 9, "childAuth": [ { "id": 14, "catename": "海信", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 15, "catename": "夏普", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 16, "catename": "创维", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 17, "catename": "TCL", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 18, "catename": "小米", "catelevel": "三级", "pid": 10, "childAuth": [] } ] }, { "id": 10, "catename": "电视", "catelevel": "二级", "pid": 9, "childAuth": [ { "id": 14, "catename": "海信", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 15, "catename": "夏普", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 16, "catename": "创维", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 17, "catename": "TCL", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 18, "catename": "小米", "catelevel": "三级", "pid": 10, "childAuth": [] } ] },
机智的我,将结果ToList(),再通过for循环,将childAuth再次去重后添加回user结果中。
[HttpGet("[action]")] public IActionResult GetCategory() { var user = _userDAL.GetTCategories().Distinct().ToList(); for (int i = 0; i < user.Count(); i++) { user[i].childAuth = user[i].childAuth.Distinct().ToList(); } if (user != null) { JsonResult json = new(new { data = user, mate = new { code = 200, msg = "获取分类列表成功" } }); return json; } else { JsonResult json = new(new { mate = new { code = 500, msg = "获取分类列表失败!" } }); ; ; return json; } }
完美去重后结果
"data": [ { "id": 9, "catename": "大家电", "catelevel": "一级", "pid": 0, "childAuth": [ { "id": 10, "catename": "电视", "catelevel": "二级", "pid": 9, "childAuth": [ { "id": 14, "catename": "海信", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 15, "catename": "夏普", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 16, "catename": "创维", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 17, "catename": "TCL", "catelevel": "三级", "pid": 10, "childAuth": [] }, { "id": 18, "catename": "小米", "catelevel": "三级", "pid": 10, "childAuth": [] } ] }, { "id": 11, "catename": "空调", "catelevel": "二级", "pid": 9, "childAuth": [ { "id": 19, "catename": "海尔", "catelevel": "三级", "pid": 11, "childAuth": [] }, { "id": 20, "catename": "海信", "catelevel": "三级", "pid": 11, "childAuth": [] }, { "id": 21, "catename": "科龙", "catelevel": "三级", "pid": 11, "childAuth": [] }, { "id": 22, "catename": "奥克斯", "catelevel": "三级", "pid": 11, "childAuth": [] }, { "id": 23, "catename": "三菱重工", "catelevel": "三级", "pid": 11, "childAuth": [] } ] },

浙公网安备 33010602011771号