Replace a List with an Arrangement

0

My query is as follows I have the following method which I show below:

public static string loadLista(string pStrOp)
{
    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["WXYZ"].ConnectionString);
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select distinct RTRIM(LTRIM(c.str_val)) as Id , RTRIM(LTRIM(c.str_val)) as Value " +
                                                                                "from dbo.toc a " +
                                                                                "inner join dbo.propval c on c.tocid = a.tocid and c.PROP_ID = 698 " +
                                                                                "where a.pset_id = 114 and c.str_val is not null " +
                                                                                "order by 2 asc;", conn);


    var items = new List<Parametro>();


    try
    {
        conn.Open();

        using (var dr = cmd.ExecuteReader())
        {
            if (dr.HasRows)
            {

                 int fc = dr.FieldCount;
                 var colums = new Dictionary<int, string>();
                 for (int i = 0; i < fc; i++)
                   colums.Add(i, dr.GetName(i));

                object[] values = new object[fc];

                while (dr.Read())
                {

                dr.GetValues(values); //Get All Values
                Parametro item = Activator.CreateInstance<Parametro>();
                var props = item.GetType().GetProperties();

                foreach (var p in props)
                {
                    foreach (var col in colums)
                    {
                        if (p.Name != col.Value) continue;
                        var value = values[col.Key];
                        if (value.GetType() != typeof(DBNull))
                        {
                            p.SetValue(item, value, null);
                        }   
                    }

                }

                items.Add(item);

                }

            }
        }
    }
    catch (Exception ex)
    {
        return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("");
    }

    return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(items);
}

As you can see I use a SqlCommand to make a query, then store it in a DataReader to process the data and finally save it in a List. The method works perfectly, my query is how I can replace the use of Lists in this method by Arrangements since I have a limitation which prevents me from using Lists. Thanks in advance for the help provided.

    
asked by ASP.NEET 27.02.2018 в 23:16
source

0 answers