BowinExtensions_Pro.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /******************************************************************
  2. ** 文 件 名: BowinExtensions_wufs.cs
  3. ** 创 建 人: wufs
  4. ** 创建日期: 2009/12/08 16:50:17
  5. ** 描 述: 提供toTable()方法
  6. ** 版 本: 1.0.0
  7. ** 修改描述:
  8. ** 修 改 人:
  9. ** 修改日期:
  10. ******************************************************************/
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Data;
  16. using System.Reflection;
  17. using System.Collections;
  18. using System.Xml;
  19. using System.Xml.Linq;
  20. using System.Dynamic;
  21. namespace Bowin.Common.Linq
  22. {
  23. public static partial class BowinExtensions
  24. {
  25. /// <summary>
  26. /// 将列表转换成以","分隔的字符串
  27. /// </summary>
  28. /// <typeparam name="T"></typeparam>
  29. /// <param name="target"></param>
  30. /// <returns></returns>
  31. public static string Join<T>(this List<T> target)
  32. {
  33. var result = new StringBuilder();
  34. target.ForEach(obj =>
  35. {
  36. if (result.Length > 0)
  37. result.Append(",");
  38. result.Append(obj);
  39. });
  40. return result.ToString();
  41. }
  42. public static List<T> Split<T>(this string target, params char[] separator)
  43. {
  44. var array = target.Split(separator);
  45. if (array.Length > 0 && !string.IsNullOrEmpty(array[0]))
  46. {
  47. List<T> list = new List<T>();
  48. foreach (var l in array)
  49. {
  50. list.Add((T)Convert.ChangeType(l, typeof(T)));
  51. }
  52. return list;
  53. }
  54. return new List<T>();
  55. }
  56. ///// <summary>
  57. ///// 用来得到结果集,支持往下一层数据,但只有一层,下一层所有方法中是类属性的
  58. ///// 都排除掉。
  59. ///// </summary>
  60. ///// <param name="source"></param>
  61. ///// <returns></returns>
  62. //public static DataTable ToTable<T>(this IQueryable<T> source) where T : class
  63. //{
  64. // DataTable dt = new DataTable("dataResult");
  65. // ArrayList al = new ArrayList(source.ToArray());
  66. // if (al.Count > 0)
  67. // {
  68. // getTableColomns(al[0], ref dt);
  69. // getTableRows(al.ToArray(), ref dt);
  70. // }
  71. // return dt;
  72. //}
  73. //public static DataTable ToTable<T>(this IEnumerable<T> source) where T : class
  74. //{
  75. // return source.AsQueryable<T>().ToTable();
  76. //}
  77. /// <summary>
  78. /// 用来得到结果集,支持往下一层数据,但只有一层,下一层所有方法中是类属性的
  79. /// 都排除掉。
  80. /// </summary>
  81. /// <param name="source"></param>
  82. /// <returns></returns>
  83. public static DataTable ToTable<T>(this IEnumerable<T> source) where T : class
  84. {
  85. DataTable dt = new DataTable("dataResult");
  86. DataSet ds = new DataSet("root");
  87. ds.Tables.Add(dt);
  88. getTableColomns(source, ref dt);
  89. var al = source.AsQueryable().ToList();
  90. if (al.Count > 0)
  91. {
  92. //getTableColomns(al.First(), ref dt);
  93. getTableRows(al, ref dt);
  94. }
  95. return dt;
  96. }
  97. public static DataTable ToTable(this IEnumerable<ExpandoObject> source)
  98. {
  99. DataTable dt = new DataTable("dataResult");
  100. DataSet ds = new DataSet("root");
  101. ds.Tables.Add(dt);
  102. var al = source.AsQueryable().ToList();
  103. if (al.Count > 0)
  104. {
  105. getTableColomns(al[0], ref dt);
  106. //getTableColomns(al.First(), ref dt);
  107. getTableRows(al, ref dt);
  108. }
  109. return dt;
  110. }
  111. /// <summary>
  112. /// 返回一个xmlelement.
  113. /// 格式为:
  114. /// [root]
  115. /// [dataResult] [linq数据] {linq值} [/linq数据] {...} [/dataResult]
  116. /// [dataResult] [linq数据] {linq值} [/linq数据] {...} [/dataResult]
  117. /// [/root]
  118. ///
  119. /// </summary>
  120. /// <typeparam name="T"></typeparam>
  121. /// <param name="source"></param>
  122. /// <returns></returns>
  123. public static XElement ToXml<T>(this IEnumerable<T> source) where T : class
  124. {
  125. DataSet ds = new DataSet("root");
  126. ds.Tables.Add(source.ToTable());
  127. //XmlDataDocument xmlroot = new XmlDataDocument(ds);
  128. return XElement.Parse(ds.GetXml());
  129. }
  130. private static void getTableColomns<T>(IEnumerable<T> source, ref DataTable dt) where T : class
  131. {
  132. Type t = source.AsQueryable<T>().ElementType;
  133. //string a = source.ToString();
  134. if (t.IsClass)
  135. {
  136. System.Reflection.PropertyInfo[] properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  137. foreach (PropertyInfo item in properties)
  138. {
  139. if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
  140. {
  141. dt.Columns.Add(item.Name, getOjectTypeOrDefault(item));
  142. }
  143. else
  144. {
  145. //object objEnd = item.GetValue(source, null);
  146. //Type t1 = objEnd.GetType();
  147. Type t1 = item.PropertyType;
  148. System.Reflection.PropertyInfo[] Itemproperties = t1.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  149. foreach (PropertyInfo detid in Itemproperties)
  150. {
  151. if (detid.PropertyType.IsValueType || detid.PropertyType.Name.StartsWith("String"))
  152. {
  153. if (!dt.Columns.Contains(detid.Name))
  154. {
  155. dt.Columns.Add(detid.Name, getOjectTypeOrDefault(detid));
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. private static void getTableColomns(ExpandoObject obj, ref DataTable dt)
  164. {
  165. var keys = ((IDictionary<string, object>)obj).Keys;
  166. foreach (var key in keys)
  167. {
  168. dt.Columns.Add(key);
  169. }
  170. }
  171. private static void getTableColomns<T>(T firstSource, ref DataTable dt) where T : class
  172. {
  173. if (firstSource == null)
  174. {
  175. return;
  176. }
  177. Type t = firstSource.GetType();
  178. if (t.IsClass)
  179. {
  180. System.Reflection.PropertyInfo[] properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  181. foreach (PropertyInfo item in properties)
  182. {
  183. if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
  184. {
  185. dt.Columns.Add(item.Name, getOjectTypeOrDefault(item));
  186. }
  187. else
  188. {
  189. object objEnd = item.GetValue(firstSource, null);
  190. Type t1 = objEnd.GetType();
  191. System.Reflection.PropertyInfo[] Itemproperties = t1.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  192. foreach (PropertyInfo detid in Itemproperties)
  193. {
  194. if (detid.PropertyType.IsValueType || detid.PropertyType.Name.StartsWith("String"))
  195. {
  196. if (!dt.Columns.Contains(detid.Name))
  197. {
  198. dt.Columns.Add(detid.Name, getOjectTypeOrDefault(detid));
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. private static Type getOjectTypeOrDefault(PropertyInfo pinfo)
  207. {
  208. if (!pinfo.PropertyType.Name.StartsWith("Nullable"))
  209. {
  210. return pinfo.PropertyType;
  211. }
  212. else
  213. {
  214. Type t = pinfo.PropertyType;
  215. Type rT = typeof(Nullable);
  216. System.Reflection.PropertyInfo Itempropertie = t.GetProperty("Value");
  217. rT = Itempropertie.PropertyType;
  218. return rT;
  219. }
  220. }
  221. private static void getTableRows<T>(List<T> Sources, ref DataTable dt) where T : class
  222. {
  223. foreach (T source in Sources)
  224. {
  225. Type t = source.GetType();
  226. DataRow cdr = dt.NewRow();
  227. if (t.IsClass)
  228. {
  229. foreach (PropertyInfo item in
  230. t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
  231. {
  232. if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
  233. {
  234. cdr[item.Name] = getOjectValueOrDefault(source, item);
  235. }
  236. else
  237. {
  238. object objEnd = item.GetValue(source, null);
  239. Type t1 = objEnd.GetType();
  240. foreach (PropertyInfo detid in t1.GetProperties(BindingFlags.Instance | BindingFlags.Public))
  241. {
  242. if (detid.PropertyType.IsValueType || detid.PropertyType.Name.StartsWith("String"))
  243. {
  244. cdr[detid.Name] = getOjectValueOrDefault(objEnd, detid);
  245. }
  246. }
  247. }
  248. }
  249. dt.Rows.Add(cdr);
  250. }
  251. }
  252. }
  253. private static void getTableRows(List<ExpandoObject> Sources, ref DataTable dt)
  254. {
  255. foreach (IDictionary<string, object> source in Sources)
  256. {
  257. DataRow dr = dt.NewRow();
  258. var keys = source.Keys;
  259. foreach (var key in keys)
  260. {
  261. dr[key] = source[key];
  262. }
  263. dt.Rows.Add(dr);
  264. }
  265. }
  266. private static object getOjectValueOrDefault(object oparent, PropertyInfo pinfo)
  267. {
  268. object result = new object();
  269. if (!pinfo.PropertyType.Name.StartsWith("Nullable"))
  270. {
  271. result = pinfo.GetValue(oparent, null);
  272. }
  273. else
  274. {
  275. System.Reflection.PropertyInfo Itempropertie = pinfo.PropertyType.GetProperty("Value");
  276. object item = pinfo.GetValue(oparent, null);
  277. if (item == null)
  278. {
  279. result = DBNull.Value;
  280. }
  281. else
  282. {
  283. object ip = Itempropertie.GetValue(item, null);
  284. result = ip == null ? DBNull.Value : ip;
  285. }
  286. }
  287. return result;
  288. }
  289. /// <summary>
  290. /// 把DATASET转成xml
  291. /// </summary>
  292. /// <param name="source"></param>
  293. /// <returns></returns>
  294. public static XElement ToXml(this DataSet source)
  295. {
  296. return XElement.Parse(source.GetXml());
  297. }
  298. public static XElement ToPageXml<T>(this IEnumerable<T> source, int pageIndex, int pageSize) where T : class
  299. {
  300. XElement root = new XElement("root");
  301. DataSet ds = new DataSet("root");
  302. ds.Tables.Add(source.AsQueryable().Paging(pageIndex, pageSize).ToTable());
  303. XElement dsroot = XElement.Parse(ds.GetXml());
  304. root.Add(new XElement("Data", dsroot.Elements("dataResult")));
  305. root.Add(new XElement("AllCount", source.AsQueryable().Count()));
  306. return root;
  307. }
  308. public static IEnumerable<TEntity> Paging<TEntity>(this IEnumerable<TEntity> entities, int pageIndex, int pageSize)
  309. {
  310. if (pageIndex < 1) pageIndex = 1;
  311. var skip = (pageIndex - 1) * pageSize;
  312. var pageentitie = entities.Skip<TEntity>(skip).Take<TEntity>(pageSize);
  313. return pageentitie;
  314. }
  315. }
  316. }