'Convert' Dictionary into List<object>

前端 未结 9 1903
感情败类
感情败类 2021-02-20 16:12

I have a Dictionary dictionary1 and I need to convert it into a List where Data has the properties lab

9条回答
  •  一向
    一向 (楼主)
    2021-02-20 16:53

    This is a old post, but post only to help other persons ;)

    Example to convert any object type:

    public List Select(string filterParam)
    {    
        DataTable dataTable = new DataTable()
    
        //{... implement filter to fill dataTable }
    
        List> rows = new List>();
        Dictionary row;
    
        foreach (DataRow dr in dataTable.Rows)
        {
            row = new Dictionary();
            foreach (DataColumn col in dataTable.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }
    
        string json = new JavaScriptSerializer().Serialize(rows);
    
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T[]));
            var tick = (T[])deserializer.ReadObject(stream);
            return tick.ToList();
        }
    }
    

提交回复
热议问题