使用Dapper从SQL返回嵌套对象

时间:2019-05-10 09:12:17

标签: c# sql-server dapper

我需要获取一个复杂的对象数组。

这是我的2个课程

public class Geometria
{
    public int idGeom { get; set; }
    public int idMatriz { get; set; }
    public string Referencia { get; set; } 
    public int toleInf { get; set; }
    public int toleSup { get; set; }
    public ICollection<MedidaX> Medidas { get; set; }
}

public class MedidaX
{
    public int IdRegGeo { get; set; }
    public float X { get; set; }
}

我作为json返回时的预期结果基本上是这样

    {
  "Geometrias": [
    {
    "idGeom": 1,
    "idMatriz": 2,
    "Referencia": "M130069",
    "toleInf": 2,
    "toleSup": 7,
    "Medidas":[
      { "IdRegGeo": 1, "X": 5.5 },
      { "IdRegGeo": 2, "X": 7.4 },
      { "IdRegGeo": 3, "X": 5.5 }
    ]
    },
    {
    "idGeom": 2,
    "idMatriz": 2,
    "Referencia": "M130070",
    "toleInf": 1,
    "toleSup": 7,
    "Medidas":[
      { "IdRegGeo": 4, "X": 3.5 },
      { "IdRegGeo": 5, "X": 4.2 },
      { "IdRegGeo": 6, "X": 7.4 }
      ]
    }
  ]
}

这是sql查询结果

 idGeom idMatriz    referencia  toleInf toleSup idRegGeo X
2002    2           M130342         2   7       1        9,81
2002    2           M130342         2   7       2        9,8
2002    2           M130342         2   7       3        3,25
2003    2           M130344         1   7       4        2
2002    2           M130342         2   7       5        1,75
2003    2           M130344         1   7       6        4,75
2002    2           M130342         2   7       7        2,78
2003    2           M130344         1   7       8        42,21
2002    2           M130342         2   7       9        7
2003    2           M130344         1   7       10       7
2002    2           M130342         2   7       11       3,55
2003    2           M130344         1   7       12       5,5

我似乎无法弄清楚如何在dapper中做到这一点,我只是将其用于简单选择,但现在我需要嵌套对象才能遍历动态图形。

编辑 我也忘了添加联接表的主键,所以我编辑来修复它

这是最终结果

// Connection String 
    private static string ConnectionString = ConfigurationManager.ConnectionStrings["NOPAPER_ConnectionString"].ConnectionString;

    [WebMethod]
    public void GetRegGeo(string matriz)
    {
        string cmdText = @"SELECT           
        hg.idGeom,          
        hg.idMatriz,
        hg.referencia,
        hg.toleInf,
        hg.toleSup,
        hrg.idRegGeo,
        ROUND(CAST((hrg.x1+hrg.x2+hrg.x3+hrg.x4)/4  AS FLOAT),2) AS X
    FROM hRegGeometrias hrg
    INNER JOIN hGeometria hg 
    ON hrg.idGeometria = hg.idGeom
    WHERE hrg.idMatriz = 2
    GROUP BY hg.referencia, hg.toleInf, hg.toleSup, hrg.x1, hrg.x2, hrg.x3, hrg.x4, hrg.idRegGeo, hg.idMatriz, hg.idGeom, hrg.idRegisto
    ORDER BY Referencia, IdRegisto ASC";

        var lookup = new Dictionary<int, Geometria>();
        using (IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NOPAPER_ConnectionString"].ConnectionString))
        {
            var multi = connection.Query<Geometria, MedidaX, Geometria>(cmdText,
                                        (geometria, medida) =>
                                        {
                                            Geometria current;
                                            if (!lookup.TryGetValue(geometria.idGeom, out current))
                                            {
                                                lookup.Add(geometria.idGeom, current = geometria);
                                                current.Medidas = new List<MedidaX>();
                                            }
                                            current.Medidas.Add(medida);
                                            return current;
                                        }, splitOn: "idRegGeo").Distinct();            
        }


        var geosList = Database.Query<Geometria>("SP_Geometrias", new { Action = "GETREGGEO", idMatriz = matriz }).ToList();
        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(geosList));
    }

像魅力一样工作:)

0 个答案:

没有答案