【问题标题】:How to deserialize a datatable to list<T>如何将数据表反序列化为 list<T>
【发布时间】:2015-02-16 16:33:08
【问题描述】:

我正在寻找一个库(最好是 nuget),它将列名与通用对象匹配的数据表转换为该对象的列表。例如,如果我有一个类:

public class foo{
   public string bar{get;set;}
   public int count{get;set;}
}

还有一个数据表

+---------+-------+
|   bar   | count |
+---------+-------+
| string1 |     1 |
| string2 |     5 |
+---------+-------+

能够调用类似的东西

List<foo> foos =DTSerializer.Deserialize<foo>(dt).ToList();

【问题讨论】:

    标签: c# serialization datatable deserialization


    【解决方案1】:

    想想你需要的是一点反射魔法而不是序列化:

    class Program
    {
        static void Main(string[] args)
        {
            DataTable table = new DataTable();
            table.Columns.Add("foo",typeof(string));
            table.Columns.Add("bar",typeof(int));
    
            table.Rows.Add("row1", 1);
            table.Rows.Add("row2", 2);
    
            var result = table.MapTableToList<foobar>();
    
            foreach (foobar item in result)
            {
                Console.WriteLine("{0}:{1}", item.foo, item.bar);
            }
    
        }
    }
    
    class foobar
    {
        public string foo { get; set; }
        public int bar { get; set; }
    }
    
    public static class ExtensionMethods
    { 
        public static List<T> MapTableToList<T>(this DataTable table) where T : new ()
        {
            List<T> result = new List<T>();
            var Type = typeof(T);
    
            foreach (DataRow row in table.Rows)
            {
                T item = new T();
                foreach (var property in Type.GetProperties())
                {
                    property.SetMethod.Invoke(item, new object[] { row[table.Columns[property.Name]] });
                }
                result.Add(item);
            }
    
            return result;
        }
    }
    

    这仅在您的列名称与属性匹配时才有效。

    【讨论】:

    • 如果它们不匹配,您仍然可以使用自定义属性来使用这种方法
    【解决方案2】:

    为 nullable 和 for 增强此扩展代码

    public static List<T> MapTableToList<T>(this DataTable table) where T : new()
    {
            List<T> result = new List<T>();
            var Type = typeof(T);
    
            foreach (DataRow row in table.Rows)
            {
                T item = new T();
                foreach (var property in Type.GetProperties())
                {
                    if(table.Columns.IndexOf(property.Name) > -1)
                    {
                        if (row[table.Columns[property.Name]] == System.DBNull.Value && property.GetMethod.ReturnType.Name.IndexOf("Nullable") > -1)
                        {
                            property.SetMethod.Invoke(item, new object[] { null });
                        }
                        else
                        {
                            property.SetMethod.Invoke(item, new object[] { row[table.Columns[property.Name]] });
                        }
                    }
                }
                result.Add(item);
            }
            return result;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多