using System.Web.Script.Serialization;using System.Collections.Generic;using System.Reflection;using System.Data;using System;namespace CommonCode{ public class Common { ////// LINQ返回DataTable类型 /// public static DataTable ToDataTable(IEnumerable 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; } /// /// list转dataset /// public static DataSet ConvertToDataSet(IList 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); public static string Serialize(DataTable dt) { List > list = new List >(); foreach (DataRow dr in dt.Rows) { Dictionary result = new Dictionary (); 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; } /// /// 转换为JSON对象 /// public static string ConventToJson(List 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; } /// /// 不需要分页 /// public static string Serialize(DataTable dt, bool flag) { JavaScriptSerializer serializer = new JavaScriptSerializer(); List> list = new List >(); foreach (DataRow dr in dt.Rows) { Dictionary result = new Dictionary (); foreach (DataColumn dc in dt.Columns) { result.Add(dc.ColumnName, dr[dc].ToString()); } list.Add(result); } return serializer.Serialize(list); } }}