/****************************************************************** ** 文 件 名: BowinExtensions_wufs.cs ** 创 建 人: wufs ** 创建日期: 2009/12/08 16:50:17 ** 描 述: 提供toTable()方法 ** 版 本: 1.0.0 ** 修改描述: ** 修 改 人: ** 修改日期: ******************************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Reflection; using System.Collections; using System.Xml; using System.Xml.Linq; using System.Dynamic; namespace Bowin.Common.Linq { public static partial class BowinExtensions { /// /// 将列表转换成以","分隔的字符串 /// /// /// /// public static string Join(this List target) { var result = new StringBuilder(); target.ForEach(obj => { if (result.Length > 0) result.Append(","); result.Append(obj); }); return result.ToString(); } public static List Split(this string target, params char[] separator) { var array = target.Split(separator); if (array.Length > 0 && !string.IsNullOrEmpty(array[0])) { List list = new List(); foreach (var l in array) { list.Add((T)Convert.ChangeType(l, typeof(T))); } return list; } return new List(); } ///// ///// 用来得到结果集,支持往下一层数据,但只有一层,下一层所有方法中是类属性的 ///// 都排除掉。 ///// ///// ///// //public static DataTable ToTable(this IQueryable source) where T : class //{ // DataTable dt = new DataTable("dataResult"); // ArrayList al = new ArrayList(source.ToArray()); // if (al.Count > 0) // { // getTableColomns(al[0], ref dt); // getTableRows(al.ToArray(), ref dt); // } // return dt; //} //public static DataTable ToTable(this IEnumerable source) where T : class //{ // return source.AsQueryable().ToTable(); //} /// /// 用来得到结果集,支持往下一层数据,但只有一层,下一层所有方法中是类属性的 /// 都排除掉。 /// /// /// public static DataTable ToTable(this IEnumerable source) where T : class { DataTable dt = new DataTable("dataResult"); DataSet ds = new DataSet("root"); ds.Tables.Add(dt); getTableColomns(source, ref dt); var al = source.AsQueryable().ToList(); if (al.Count > 0) { //getTableColomns(al.First(), ref dt); getTableRows(al, ref dt); } return dt; } public static DataTable ToTable(this IEnumerable source) { DataTable dt = new DataTable("dataResult"); DataSet ds = new DataSet("root"); ds.Tables.Add(dt); var al = source.AsQueryable().ToList(); if (al.Count > 0) { getTableColomns(al[0], ref dt); //getTableColomns(al.First(), ref dt); getTableRows(al, ref dt); } return dt; } /// /// 返回一个xmlelement. /// 格式为: /// [root] /// [dataResult] [linq数据] {linq值} [/linq数据] {...} [/dataResult] /// [dataResult] [linq数据] {linq值} [/linq数据] {...} [/dataResult] /// [/root] /// /// /// /// /// public static XElement ToXml(this IEnumerable source) where T : class { DataSet ds = new DataSet("root"); ds.Tables.Add(source.ToTable()); //XmlDataDocument xmlroot = new XmlDataDocument(ds); return XElement.Parse(ds.GetXml()); } private static void getTableColomns(IEnumerable source, ref DataTable dt) where T : class { Type t = source.AsQueryable().ElementType; //string a = source.ToString(); if (t.IsClass) { System.Reflection.PropertyInfo[] properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (PropertyInfo item in properties) { if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { dt.Columns.Add(item.Name, getOjectTypeOrDefault(item)); } else { //object objEnd = item.GetValue(source, null); //Type t1 = objEnd.GetType(); Type t1 = item.PropertyType; System.Reflection.PropertyInfo[] Itemproperties = t1.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (PropertyInfo detid in Itemproperties) { if (detid.PropertyType.IsValueType || detid.PropertyType.Name.StartsWith("String")) { if (!dt.Columns.Contains(detid.Name)) { dt.Columns.Add(detid.Name, getOjectTypeOrDefault(detid)); } } } } } } } private static void getTableColomns(ExpandoObject obj, ref DataTable dt) { var keys = ((IDictionary)obj).Keys; foreach (var key in keys) { dt.Columns.Add(key); } } private static void getTableColomns(T firstSource, ref DataTable dt) where T : class { if (firstSource == null) { return; } Type t = firstSource.GetType(); if (t.IsClass) { System.Reflection.PropertyInfo[] properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (PropertyInfo item in properties) { if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { dt.Columns.Add(item.Name, getOjectTypeOrDefault(item)); } else { object objEnd = item.GetValue(firstSource, null); Type t1 = objEnd.GetType(); System.Reflection.PropertyInfo[] Itemproperties = t1.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (PropertyInfo detid in Itemproperties) { if (detid.PropertyType.IsValueType || detid.PropertyType.Name.StartsWith("String")) { if (!dt.Columns.Contains(detid.Name)) { dt.Columns.Add(detid.Name, getOjectTypeOrDefault(detid)); } } } } } } } private static Type getOjectTypeOrDefault(PropertyInfo pinfo) { if (!pinfo.PropertyType.Name.StartsWith("Nullable")) { return pinfo.PropertyType; } else { Type t = pinfo.PropertyType; Type rT = typeof(Nullable); System.Reflection.PropertyInfo Itempropertie = t.GetProperty("Value"); rT = Itempropertie.PropertyType; return rT; } } private static void getTableRows(List Sources, ref DataTable dt) where T : class { foreach (T source in Sources) { Type t = source.GetType(); DataRow cdr = dt.NewRow(); if (t.IsClass) { foreach (PropertyInfo item in t.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { cdr[item.Name] = getOjectValueOrDefault(source, item); } else { object objEnd = item.GetValue(source, null); Type t1 = objEnd.GetType(); foreach (PropertyInfo detid in t1.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (detid.PropertyType.IsValueType || detid.PropertyType.Name.StartsWith("String")) { cdr[detid.Name] = getOjectValueOrDefault(objEnd, detid); } } } } dt.Rows.Add(cdr); } } } private static void getTableRows(List Sources, ref DataTable dt) { foreach (IDictionary source in Sources) { DataRow dr = dt.NewRow(); var keys = source.Keys; foreach (var key in keys) { dr[key] = source[key]; } dt.Rows.Add(dr); } } private static object getOjectValueOrDefault(object oparent, PropertyInfo pinfo) { object result = new object(); if (!pinfo.PropertyType.Name.StartsWith("Nullable")) { result = pinfo.GetValue(oparent, null); } else { System.Reflection.PropertyInfo Itempropertie = pinfo.PropertyType.GetProperty("Value"); object item = pinfo.GetValue(oparent, null); if (item == null) { result = DBNull.Value; } else { object ip = Itempropertie.GetValue(item, null); result = ip == null ? DBNull.Value : ip; } } return result; } /// /// 把DATASET转成xml /// /// /// public static XElement ToXml(this DataSet source) { return XElement.Parse(source.GetXml()); } public static XElement ToPageXml(this IEnumerable source, int pageIndex, int pageSize) where T : class { XElement root = new XElement("root"); DataSet ds = new DataSet("root"); ds.Tables.Add(source.AsQueryable().Paging(pageIndex, pageSize).ToTable()); XElement dsroot = XElement.Parse(ds.GetXml()); root.Add(new XElement("Data", dsroot.Elements("dataResult"))); root.Add(new XElement("AllCount", source.AsQueryable().Count())); return root; } public static IEnumerable Paging(this IEnumerable entities, int pageIndex, int pageSize) { if (pageIndex < 1) pageIndex = 1; var skip = (pageIndex - 1) * pageSize; var pageentitie = entities.Skip(skip).Take(pageSize); return pageentitie; } } }