首页 > 代码库 > LINQ返回DataTable类型 list转dataset 转换为JSON对象

LINQ返回DataTable类型 list转dataset 转换为JSON对象

using System.Web.Script.Serialization;using System.Collections.Generic;using System.Reflection;using System.Data;using System;namespace CommonCode{    public class Common    {        /// <summary>         /// LINQ返回DataTable类型        /// </summary>         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;        }        /// <summary>        /// list转dataset        /// </summary>        public static DataSet ConvertToDataSet<T>(IList<T> list)        {            if (list == null || list.Count <= 0)                return null;            DataSet ds = new DataSet();            DataTable dt = new DataTable(typeof(T).Name);            DataColumn column;            DataRow row;            System.Reflection.PropertyInfo[] myPropertyInfo =                typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);            foreach (T t in list)            {                if (t == null) continue;                row = dt.NewRow();                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)                {                    System.Reflection.PropertyInfo pi = myPropertyInfo[i];                    String name = pi.Name;                    if (dt.Columns[name] == null)                    {                        if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")                        {                            column = new DataColumn(name, typeof(Int32));                            dt.Columns.Add(column);                            //row[name] = pi.GetValue(t, new object[] {i});//PropertyInfo.GetValue(object,object[])                            if (pi.GetValue(t, null) != null)                                row[name] = pi.GetValue(t, null);                            else                                row[name] = System.DBNull.Value;                        }                        else if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.DateTime]")                        {                            column = new DataColumn(name, typeof(DateTime));                            dt.Columns.Add(column);                            if (pi.GetValue(t, null) != null)                                row[name] = pi.GetValue(t, null);                            else                                row[name] = System.DBNull.Value;                        }                        else                        {                            column = new DataColumn(name, pi.PropertyType);                            dt.Columns.Add(column);                            row[name] = pi.GetValue(t, null);                        }                    }                }                dt.Rows.Add(row);            }            ds.Tables.Add(dt);            return ds;        }        public delegate object[] CreateRowDelegate<T>(T t);        public static string Serialize(DataTable dt)        {            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();            foreach (DataRow dr in dt.Rows)            {                Dictionary<string, object> result = new Dictionary<string, object>();                foreach (DataColumn dc in dt.Columns)                {                    result.Add(dc.ColumnName, dr[dc].ToString());                }                list.Add(result);            }            int count = 0;            try            {                count = Convert.ToInt32(dt.TableName);            }            catch (System.Exception ex)            {                count = dt.Rows.Count;            }            string strReturn = "";            if (count == 0)            {                strReturn = "{\"totalCount\":0,\"data\":[]}";            }            else            {                strReturn = ConventToJson(list, count);            }            return strReturn;        }        /// <summary>        /// 转换为JSON对象        /// </summary>        public static string ConventToJson<T>(List<T> list, int count)        {            JavaScriptSerializer serializer = new JavaScriptSerializer();            string strJson = serializer.Serialize(list);            strJson = strJson.Substring(1);            strJson = strJson.Insert(0, "{totalCount:" + count + ",data:[");            strJson += "}";            return strJson;        }        /// <summary>        /// 不需要分页        /// </summary>        public static string Serialize(DataTable dt, bool flag)        {            JavaScriptSerializer serializer = new JavaScriptSerializer();            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();            foreach (DataRow dr in dt.Rows)            {                Dictionary<string, object> result = new Dictionary<string, object>();                foreach (DataColumn dc in dt.Columns)                {                    result.Add(dc.ColumnName, dr[dc].ToString());                }                list.Add(result);            }            return serializer.Serialize(list);        }    }}

 

LINQ返回DataTable类型 list转dataset 转换为JSON对象