123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- /******************************************************************
- ** 文 件 名: 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
- {
- /// <summary>
- /// 将列表转换成以","分隔的字符串
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="target"></param>
- /// <returns></returns>
- public static string Join<T>(this List<T> target)
- {
- var result = new StringBuilder();
- target.ForEach(obj =>
- {
- if (result.Length > 0)
- result.Append(",");
- result.Append(obj);
- });
- return result.ToString();
- }
- public static List<T> Split<T>(this string target, params char[] separator)
- {
- var array = target.Split(separator);
- if (array.Length > 0 && !string.IsNullOrEmpty(array[0]))
- {
- List<T> list = new List<T>();
- foreach (var l in array)
- {
- list.Add((T)Convert.ChangeType(l, typeof(T)));
- }
- return list;
- }
- return new List<T>();
- }
- ///// <summary>
- ///// 用来得到结果集,支持往下一层数据,但只有一层,下一层所有方法中是类属性的
- ///// 都排除掉。
- ///// </summary>
- ///// <param name="source"></param>
- ///// <returns></returns>
- //public static DataTable ToTable<T>(this IQueryable<T> 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<T>(this IEnumerable<T> source) where T : class
- //{
- // return source.AsQueryable<T>().ToTable();
- //}
- /// <summary>
- /// 用来得到结果集,支持往下一层数据,但只有一层,下一层所有方法中是类属性的
- /// 都排除掉。
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static DataTable ToTable<T>(this IEnumerable<T> 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<ExpandoObject> 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;
- }
- /// <summary>
- /// 返回一个xmlelement.
- /// 格式为:
- /// [root]
- /// [dataResult] [linq数据] {linq值} [/linq数据] {...} [/dataResult]
- /// [dataResult] [linq数据] {linq值} [/linq数据] {...} [/dataResult]
- /// [/root]
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source"></param>
- /// <returns></returns>
- public static XElement ToXml<T>(this IEnumerable<T> 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<T>(IEnumerable<T> source, ref DataTable dt) where T : class
- {
- Type t = source.AsQueryable<T>().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<string, object>)obj).Keys;
- foreach (var key in keys)
- {
- dt.Columns.Add(key);
- }
- }
- private static void getTableColomns<T>(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<T>(List<T> 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<ExpandoObject> Sources, ref DataTable dt)
- {
- foreach (IDictionary<string, object> 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;
- }
- /// <summary>
- /// 把DATASET转成xml
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static XElement ToXml(this DataSet source)
- {
- return XElement.Parse(source.GetXml());
- }
- public static XElement ToPageXml<T>(this IEnumerable<T> 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<TEntity> Paging<TEntity>(this IEnumerable<TEntity> entities, int pageIndex, int pageSize)
- {
- if (pageIndex < 1) pageIndex = 1;
- var skip = (pageIndex - 1) * pageSize;
- var pageentitie = entities.Skip<TEntity>(skip).Take<TEntity>(pageSize);
- return pageentitie;
- }
- }
- }
|