¿A quién convertir Dapper resultado QueryMultiple a Json en mvc?

asp.net-mvc c# dapper json sql

Pregunta

Tengo 3 seleccione así. Tengo todos los 3 select en sql convertir a json.

public IHttpActionResult GetTest()
{
        string sql = $"SELECT 'Ahmad' AS N'Name',\n" +
        "1 AS N'Code',\n" +
        "0914 AS N'Phone' FOR JSON PATH\n" +
        "SELECT 'Alizadeh' AS N'NameFamily',\n" +
        "2 AS N'Code',\n" +
        "0915 AS N'Phone' FOR JSON PATH\n" +
        " \n" +
        " SELECT 'Seven' AS N'BookName',\n" +
        "3 AS N'Qty',\n" +
        "0916 AS N'IBS' FOR JSON PATH";
         var info = Connections.QueryMultiple(sql);
        return Ok(info);
 }

Resultado como este:

[
  {
    "Name": "Ahmad",
    "Code": 1,
    "Phone": 914
  },
  {
    "NameFamily": "Alizadeh",
    "Code": 2,
    "Phone": 915
  },
  {
    "BookName": "Seven",
    "Qty": 3,
    "IBS": 916
  }
]

Respuesta aceptada

Para leer todo.

public IHttpActionResult GetTest()
{

    string sql = $"SELECT 'Ahmad' AS N'Name',\n" +
        "1 AS N'Code',\n" +
        "0914 AS N'Phone' FOR JSON PATH\n" +
        "SELECT 'Alizadeh' AS N'NameFamily',\n" +
        "2 AS N'Code',\n" +
        "0915 AS N'Phone' FOR JSON PATH\n" +
        " \n" +
        " SELECT 'Seven' AS N'BookName',\n" +
        "3 AS N'Qty',\n" +
        "0916 AS N'IBS' FOR JSON PATH";
    var info = Connections.SaleBranch.SqlConn.QueryMultiple(sql);
    var lstResult = new List<dynamic>();
    var isNext = false;
    do{
        var first2 = info.Read<dynamic>().Single();
        lstResult.Add(first2);
        isNext=info.IsConsumed;
    }
    while (!isNext);

    return Ok(lstResult);
}

Respuesta popular

Si usa Azure / SQL2016 o posterior, puede usar

PARA JSON Consulte este artículo: https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server



Licencia bajo: CC-BY-SA with attribution
No afiliado con Stack Overflow
¿Es esto KB legal? Sí, aprende por qué
Licencia bajo: CC-BY-SA with attribution
No afiliado con Stack Overflow
¿Es esto KB legal? Sí, aprende por qué