首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

哪位高手能帮小弟我分析下面的代码中ConvertToDataTable和 ConvertToList这两个方法,看不懂,查Msdn也没搞明白

2013-11-23 
谁能帮我分析下面的代码中ConvertToDataTable和 ConvertToList这两个方法,看不懂,查Msdn也没搞明白。本帖最

谁能帮我分析下面的代码中ConvertToDataTable和 ConvertToList这两个方法,看不懂,查Msdn也没搞明白。
本帖最后由 Y2012 于 2013-11-15 17:23:55 编辑


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form2 : Form
    {
        class Item
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public double Value { get; set; }
            public DateTime UpdateDateTime { get; set; }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            var list = new List<Item>();
            for (int i = 0; i < 20; i++)
            {
                list.Add(new Item()
                {
                    ID = i,
                    Name = "Name of " + i.ToString(),
                    Value = i,
                    UpdateDateTime = DateTime.Now
                });
            }
            dataGridView1.DataSource = list;
            List<Item> nlist = (List<Item>)dataGridView1.DataSource;
            var resultTable = ConvertToDataTable<List<Item>>(nlist);

            var resultList = ConvertToList<List<Item>>(resultTable);
        }

        private static DataTable ConvertToDataTable<T>(T list) where T : IList
        {
            var table = new DataTable(typeof(T).GetGenericArguments()[0].Name);
            typeof(T).GetGenericArguments()[0].GetProperties().
                ToList().ForEach(p => table.Columns.Add(p.Name, p.PropertyType));
            foreach (var item in list)
            {
                var row = table.NewRow();
                item.GetType().GetProperties().
                    ToList().ForEach(p => row[p.Name] = p.GetValue(item, null));
                table.Rows.Add(row);
            }
            return table;
        }
        private static T ConvertToList<T>(DataTable table) where T : IList, new()
        {
            var list = new T();


            foreach (DataRow row in table.Rows)
            {
                var item = Activator.CreateInstance(typeof(T).GetGenericArguments()[0]);
                list.GetType().GetGenericArguments()[0].GetProperties().
                    ToList().ForEach(p => p.SetValue(item, row[p.Name], null));
                list.Add(item);
            }
            return list;
        }
    }
}

c# 反射 lambda
[解决办法]
http://blog.csdn.net/zx13525079024/article/details/4962738
[解决办法]
private static DataTable ConvertToDataTable<T>(T list) where T : IList//泛型参数类型限定T为IList类型
        {
            var table = new DataTable(typeof(T).GetGenericArguments()[0].Name);//通过反射获得DataTable的Name
            typeof(T).GetGenericArguments()[0].GetProperties().
                ToList().ForEach(p => table.Columns.Add(p.Name, p.PropertyType));//通过反射获得属性名并且循环的初始化DataTable(列名,属性类型)
            foreach (var item in list)
            {
                var row = table.NewRow();
                item.GetType().GetProperties().
                    ToList().ForEach(p => row[p.Name] = p.GetValue(item, null));//lambda写法,将属性值循环的加入到DataTable的Row的每一列当中
                table.Rows.Add(row);//加入到这个DataTable中
            }
            return table;//返回转换好的DataTable
        }

热点排行