ConvertHelper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using Vinno.vCloud.Protocol.Infrastructures;
  8. namespace ReportDataResumeTool.Common
  9. {
  10. public class ConvertHelper
  11. {
  12. /// <summary>
  13. /// list转化为table
  14. /// </summary>
  15. /// <typeparam name="T"></typeparam>
  16. /// <param name="entitys"></param>
  17. /// <returns></returns>
  18. public static DataTable ListToDataTable<T>(IList<T> dataList)
  19. {
  20. //检查实体集合不能为空
  21. if (dataList == null || dataList.Count < 1)
  22. {
  23. return new DataTable();
  24. }
  25. //取出第一个实体的所有Propertie
  26. Type entityType = dataList[0].GetType();
  27. PropertyInfo[] entityProperties = entityType.GetProperties();
  28. //生成DataTable的structure
  29. //生产代码中,应将生成的DataTable结构Cache起来,此处略
  30. DataTable dt = new DataTable("dt");
  31. for (int i = 0; i < entityProperties.Length; i++)
  32. {
  33. //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
  34. dt.Columns.Add(entityProperties[i].Name);
  35. }
  36. //将所有entity添加到DataTable中
  37. foreach (object entity in dataList)
  38. {
  39. //检查所有的的实体都为同一类型
  40. if (entity.GetType() != entityType)
  41. {
  42. throw new Exception("要转换的集合元素类型不一致");
  43. }
  44. object[] entityValues = new object[entityProperties.Length];
  45. for (int i = 0; i < entityProperties.Length; i++)
  46. {
  47. entityValues[i] = entityProperties[i].GetValue(entity, null);
  48. }
  49. dt.Rows.Add(entityValues);
  50. }
  51. return dt;
  52. }
  53. /// <summary>
  54. /// datatable转list
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="table"></param>
  58. /// <returns></returns>
  59. public static IList<T> DataTableToList<T>(DataTable table) where T : class, new()
  60. {
  61. IList<T> list = new List<T>();
  62. foreach (DataRow row in table.Rows)
  63. {
  64. T obj = new T();
  65. foreach (PropertyInfo info in obj.GetType().GetProperties())
  66. {
  67. if (table.Columns.Contains(info.Name))
  68. {
  69. if (row[info.Name] != DBNull.Value)
  70. {
  71. info.SetValue(obj, row[info.Name], null);
  72. }
  73. }
  74. }
  75. list.Add(obj);
  76. }
  77. return list;
  78. }
  79. /// <summary>
  80. /// utc转bj时间
  81. /// </summary>
  82. public static DateTime UTCToBeijing(DateTime utcTime)
  83. {
  84. // 获取中国标准时间 (CST) 的时区信息
  85. TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
  86. // 将UTC时间转换为北京时间
  87. DateTime beijingTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, cstZone);
  88. return beijingTime;
  89. }
  90. /// <summary>
  91. /// 时间戳转UTC时间
  92. /// </summary>
  93. public static DateTime GetUTCTimeFromUnixTime(long timestamp)
  94. {
  95. DateTime utcDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  96. DateTime beijingTime = utcDateTime.AddMilliseconds(timestamp).ToUniversalTime();
  97. return beijingTime;
  98. }
  99. /// <summary>
  100. /// 时间戳转北京时间
  101. /// </summary>
  102. public static DateTime GetBeiJingTimeFromUnixTime(long timestamp)
  103. {
  104. DateTime utcDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  105. DateTime beijingTime = utcDateTime.AddMilliseconds(timestamp).ToUniversalTime().AddHours(8);
  106. return beijingTime;
  107. }
  108. /// <summary>
  109. /// 通过身份证查询出生日期
  110. /// </summary>
  111. /// <param name="b_car_code"></param>
  112. /// <returns></returns>
  113. public static string GetBirthdayByCode(string b_car_code)
  114. {
  115. string birthday = "";
  116. //处理18位的身份证号码从号码中得到生日和性别代码
  117. if (b_car_code.Length == 18)
  118. {
  119. //110101199001011234
  120. birthday = b_car_code.Substring(6, 4) + "-" + b_car_code.Substring(10, 2) + "-" + b_car_code.Substring(12, 2);
  121. }
  122. //处理15位的身份证号码从号码中得到生日和性别代码
  123. if (b_car_code.Length == 15)
  124. {
  125. birthday = "19" + b_car_code.Substring(6, 2) + "-" + b_car_code.Substring(8, 2) + "-" + b_car_code.Substring(10, 2);
  126. }
  127. return birthday;
  128. }
  129. /// <summary>
  130. /// 通过身份证查询出生日期
  131. /// </summary>
  132. /// <param name="b_car_code"></param>
  133. /// <returns></returns>
  134. public static GenderType GetSexByCode(string b_car_code)
  135. {
  136. GenderType genderType = GenderType.Unassigned;
  137. //处理18位的身份证号码从号码中得到生日和性别代码
  138. if (b_car_code.Length == 18)
  139. {
  140. int genderDigit = int.Parse(b_car_code.Substring(16, 1));
  141. genderType = genderDigit % 2 == 0 ? GenderType.Female : GenderType.Male;
  142. }
  143. //处理15位的身份证号码从号码中得到生日和性别代码
  144. if (b_car_code.Length == 15)
  145. {
  146. int genderDigit = int.Parse(b_car_code.Substring(14, 1));
  147. genderType = genderDigit % 2 == 0 ? GenderType.Female : GenderType.Male;
  148. }
  149. return genderType;
  150. }
  151. /// <summary>
  152. /// 获取计算年龄
  153. /// </summary>
  154. /// <param name="birthdate"></param>
  155. /// <returns></returns>
  156. public static int CalculateAge(DateTime birthdate)
  157. {
  158. DateTime now = DateTime.Today;
  159. int age = now.Year - birthdate.Year;
  160. if (now < birthdate.AddYears(age))
  161. age--;
  162. return age;
  163. }
  164. }
  165. }