zoukankan      html  css  js  c++  java
  • LinqToDataTable

    /// <summary>
     ///  下面通过一个方法来实现返回DataTable类型 
     /// LINQ返回DataTable类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="varlist"></param>
    /// <returns></returns>
            public static DataTable ToDataTable<T>(IEnumerable<T> varlist)
            {
                DataTable dtReturn = new DataTable();
                // column names 
                PropertyInfo[] oProps = null;
                if (varlist == null)
                    return dtReturn;
                foreach (T rec in varlist)
                {
                    if (oProps == null)
                    {
                       oProps = ((Type)rec.GetType()).GetProperties();
                        foreach (PropertyInfo pi in oProps)
                        {
                            Type colType = pi.PropertyType;
                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                                 == typeof(Nullable<>)))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                            dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                        }
                    }
                    DataRow dr = dtReturn.NewRow();
                    foreach (PropertyInfo pi in oProps)
                    {
                        dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                        (rec, null);
                    }
                    dtReturn.Rows.Add(dr);
                }
                return dtReturn;
            }
    

    public static System.Data.DataTable LinqToDataTable<T>(IEnumerable<T> data)
            {
                var dt = new System.Data.DataTable();
                var ps = typeof(T).GetProperties().ToList();
                ps.ForEach(p => dt.Columns.Add(p.Name, p.PropertyType));

                foreach (T t in data)
                {
                    var dr = dt.NewRow();
                    var vs = from p in ps select p.GetValue(t, null);
                    var ls = vs.ToList();
                    int i = 0;
                    ls.ForEach(c => dr[i++] = c);
                    dt.Rows.Add(dr);
                }
                return dt;
            }

            public static System.Data.DataTable ToDataTable<T>(IEnumerable<T> data)
            {
                var dt = new System.Data.DataTable();
                var ps = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T));
                foreach (System.ComponentModel.PropertyDescriptor dp in ps)
                    dt.Columns.Add(dp.Name, dp.PropertyType);
                foreach (T t in data)
                {
                    var dr = dt.NewRow();
                    foreach (System.ComponentModel.PropertyDescriptor dp in ps)
                        dr[dp.Name] = dp.GetValue(t);
                    dt.Rows.Add(dr);
                }
                return dt;
            }

  • 相关阅读:
    python中的编码问题
    CVPR2018 Tutorial 之 Visual Recognition and Beyond
    hdu 1376 Octal Fractions
    hdu 1329 Hanoi Tower Troubles Again!
    hdu 1309 Loansome Car Buyer
    hdu 1333 Smith Numbers
    hdu 1288 Hat's Tea
    hdu 1284 钱币兑换问题
    hdu 1275 两车追及或相遇问题
    hdu 1270 小希的数表
  • 原文地址:https://www.cnblogs.com/volts0302/p/5235843.html
Copyright © 2011-2022 走看看