在C#Aspnet WebApi结果对象上添加自定义键

时间:2014-06-20 15:56:39

标签: c# asp.net json asp.net-web-api asp.net-web-api2

我必须创建Asp.Net WebApi。

我有这个结构化对象

[Serializable()]
public partial class OrderList
{
    [Key]
    public string ClienteId;
    public List<Ordine> Ordini;
}

public class Ordine
{
    [Key]
    public string CodOrd;
    public string Stato;
    public string Pagamento;
    public List<Riga> Righe;
}

public class Riga
{
    public string Articolo;
    public string Quantita;
    public string Listino;
}

我用这段代码填充这个对象:

public IEnumerable<OrderList> Get()
{
    List<OrderList> opDataList = new List<OrderList>();

    string sqlClienti = " SELECT CODICE FROM CLIENTI ";

    string connStr = ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(sqlClienti, conn))
        {
            conn.Open();
            using (SqlDataReader app_dr = cmd.ExecuteReader())
            {
                while(app_dr.Read())
                {
                    OrderList itemToAdd = new OrderList();
                    itemToAdd.ClienteId = app_dr["CODICE"].ToString();

                    string sqlOrdine = " SELECT * FROM Order ";
                    sqlOrdine += " WHERE CODCLI = '" + app_dr["CODICE"].ToString() + "'";
                    List<Ordine> orderListToadd = new List<Ordine>();

                    using (SqlConnection conn_1 = new SqlConnection(connStr))
                    {
                        using (SqlCommand cmd_1 = new SqlCommand(sqlOrdine, conn_1))
                        {
                            conn_1.Open();
                            using (SqlDataReader app_dr_1 = cmd_1.ExecuteReader())
                            {
                                while (app_dr_1.Read())
                                {
                                    Ordine orderToAdd = new Ordine();
                                    orderToAdd.CodOrd = app_dr_1["CODORD"].ToString();
                                    orderToAdd.Stato = app_dr_1["STATO"].ToString();

                                    List<Riga> righeListToadd = new List<Riga>();
                                    string sqlDettaglio = " SELECT FROM  RIGORD WHERE BI.NUMMXL='" + app_dr_1["NUMFAT"].ToString() + "' AND BI.DATMXL='" + app_dr_1["DATMXL"].ToString() + "' ";

                                    using (SqlConnection conn_2 = new SqlConnection(connStr))
                                    {
                                        using (SqlCommand cmd_2 = new SqlCommand(sqlDettaglio, conn_2))
                                        {
                                            conn_2.Open();
                                            using (SqlDataReader app_dr_2 = cmd_2.ExecuteReader())
                                            {
                                                while (app_dr_2.Read())
                                                {
                                                    Riga rigaToAdd = new Riga();
                                                    rigaToAdd.Articolo = app_dr_2["ARTICOLO"].ToString();
                                                    rigaToAdd.Listino = app_dr_2["LISTINO1"].ToString();
                                                    rigaToAdd.Quantita = app_dr_2["QUANTITA"].ToString();
                                                    righeListToadd.Add(rigaToAdd);
                                                }
                                            }
                                            conn_2.Close();
                                        }
                                    }
                                    orderToAdd.Righe = righeListToadd;
                                    orderListToadd.Add(orderToAdd);
                                }
                            }
                            conn_1.Close();
                        }
                    }
                    itemToAdd.Ordini = orderListToadd;
                    opDataList.Add(itemToAdd);
                }
            }
            conn.Close();
        }
    }
    return opDataList;
}

一切正常,我有这个结果

[
    {
        "ClienteId": "3458",
        "Ordini": [
            {
                "CodOrd": "OW_285",
                "Stato": "EVASO",
                "Pagamento": "CONTRASSEGNO AL CORRIERE",
                "Righe": [
                    {
                        "Articolo": "5114-21",
                        "Quantita": "6",
                        "Listino": "2,8"
                    },
                    {
                        "Articolo": "T1057",
                        "Quantita": "6",
                        "Listino": "12,8"
                    }
                ]
            }
        ]
    },
    {
        "ClienteId": "1297",
        "Ordini": [
            {
                "CodOrd": "OC_1/2985",
                "Stato": "EVASO",
                "Pagamento": "CONTRASSEGNO AL CORRIERE",
                "Righe": [
                    {
                        "Articolo": "B892VR",
                        "Quantita": "12",
                        "Listino": "2,5"
                    },
                    {
                        "Articolo": "B895VR",
                        "Quantita": "12",
                        "Listino": "3,2"
                    }
                ]
            }
        ]
    }
]

但我会这样:

    {
    "1297": {
        "ClienteId": "1297",
        "Ordini": {
            "OC_1/2985": {
                "CodOrd": "OC_1/2985",
                "Stato": "EVASO",
                "Pagamento": "CONTRASSEGNO AL CORRIERE",
                "Righe": [
                    {
                        "Articolo": "B892VR",
                        "Quantita": "12",
                        "Listino": "2,5"
                    },
                    {
                        "Articolo": "B895VR",
                        "Quantita": "12",
                        "Listino": "3,2"
                    }
                ]
            }
        }
    },
    "3458": {
        "ClienteId": "3458",
        "Ordini": {
            "OW_285": {
                "CodOrd": "OW_285",
                "Stato": "EVASO",
                "Pagamento": "CONTRASSEGNO AL CORRIERE",
                "Righe": [
                    {
                        "Articolo": "5114-21",
                        "Quantita": "6",
                        "Listino": "2,8"
                    },
                    {
                        "Articolo": "T1057",
                        "Quantita": "6",
                        "Listino": "12,8"
                    }
                ]
            }
        }
    }
}

如何在我的结构上添加对象键?

0 个答案:

没有答案