|
@@ -43,88 +43,29 @@ namespace Masuit.Tools.Database
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// DataTable转换成实体列表
|
|
|
+ /// datatable转List
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="T">实体 T </typeparam>
|
|
|
- /// <param name="table">datatable</param>
|
|
|
- /// <returns>强类型的数据集合</returns>
|
|
|
- public static IList<T> DataTableToList<T>(this DataTable table) where T : class
|
|
|
- {
|
|
|
- if (!HasRows(table))
|
|
|
- {
|
|
|
- return new List<T>();
|
|
|
- }
|
|
|
-
|
|
|
- IList<T> list = new List<T>();
|
|
|
- foreach (DataRow dr in table.Rows)
|
|
|
- {
|
|
|
- var model = Activator.CreateInstance<T>();
|
|
|
-
|
|
|
- foreach (DataColumn dc in dr.Table.Columns)
|
|
|
- {
|
|
|
- object drValue = dr[dc.ColumnName];
|
|
|
- PropertyInfo pi = model.GetType().GetProperty(dc.ColumnName);
|
|
|
-
|
|
|
- if (pi != null && pi.CanWrite && (drValue != null && !Convert.IsDBNull(drValue)))
|
|
|
- {
|
|
|
- pi.SetValue(model, drValue, null);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- list.Add(model);
|
|
|
- }
|
|
|
-
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 实体列表转换成DataTable
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">实体</typeparam>
|
|
|
- /// <param name="list"> 实体列表</param>
|
|
|
- /// <returns>映射为数据表</returns>
|
|
|
- /// <exception cref="OverflowException">The array is multidimensional and contains more than <see cref="F:System.Int32.MaxValue" /> elements.</exception>
|
|
|
- public static DataTable ListToDataTable<T>(this IList<T> list) where T : class
|
|
|
+ /// <typeparam name="T"></typeparam>
|
|
|
+ /// <param name="dt"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static List<T> ToList<T>(this DataTable dt) where T : class, new()
|
|
|
{
|
|
|
- if (list == null || list.Count <= 0)
|
|
|
+ List<T> list = new List<T>();
|
|
|
+ using (dt)
|
|
|
{
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- var dt = new DataTable(typeof(T).Name);
|
|
|
- PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
|
- int length = myPropertyInfo.Length;
|
|
|
- bool createColumn = true;
|
|
|
- foreach (T t in list)
|
|
|
- {
|
|
|
- if (t == null)
|
|
|
+ if (dt == null || dt.Rows.Count == 0)
|
|
|
{
|
|
|
- continue;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
- var row = dt.NewRow();
|
|
|
- for (int i = 0; i < length; i++)
|
|
|
+ DataTableBuilder<T> eblist = DataTableBuilder<T>.CreateBuilder(dt.Rows[0]);
|
|
|
+ foreach (DataRow info in dt.Rows)
|
|
|
{
|
|
|
- PropertyInfo pi = myPropertyInfo[i];
|
|
|
- string name = pi.Name;
|
|
|
- if (createColumn)
|
|
|
- {
|
|
|
- var column = new DataColumn(name, pi.PropertyType);
|
|
|
- dt.Columns.Add(column);
|
|
|
- }
|
|
|
-
|
|
|
- row[name] = pi.GetValue(t, null);
|
|
|
+ list.Add(eblist.Build(info));
|
|
|
}
|
|
|
|
|
|
- if (createColumn)
|
|
|
- {
|
|
|
- createColumn = false;
|
|
|
- }
|
|
|
-
|
|
|
- dt.Rows.Add(row);
|
|
|
+ return list;
|
|
|
}
|
|
|
-
|
|
|
- return dt;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -354,53 +295,12 @@ namespace Masuit.Tools.Database
|
|
|
|
|
|
DataTable dt = rows[0].Table.Clone();
|
|
|
dt.DefaultView.Sort = rows[0].Table.DefaultView.Sort;
|
|
|
- for (int i = 0; i < rows.Length; i++)
|
|
|
+ foreach (var t in rows)
|
|
|
{
|
|
|
- dt.LoadDataRow(rows[i].ItemArray, true);
|
|
|
+ dt.LoadDataRow(t.ItemArray, true);
|
|
|
}
|
|
|
|
|
|
return dt;
|
|
|
}
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 排序表的视图
|
|
|
- /// </summary>
|
|
|
- /// <param name="dt">原内存表</param>
|
|
|
- /// <param name="sorts">排序方式</param>
|
|
|
- /// <returns>排序后的内存表</returns>
|
|
|
- public static DataTable SortedTable(this DataTable dt, params string[] sorts)
|
|
|
- {
|
|
|
- if (dt.Rows.Count > 0)
|
|
|
- {
|
|
|
- string tmp = "";
|
|
|
- for (int i = 0; i < sorts.Length; i++)
|
|
|
- {
|
|
|
- tmp += sorts[i] + ",";
|
|
|
- }
|
|
|
-
|
|
|
- dt.DefaultView.Sort = tmp.TrimEnd(',');
|
|
|
- }
|
|
|
-
|
|
|
- return dt;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 根据条件过滤表的内容
|
|
|
- /// </summary>
|
|
|
- /// <param name="dt">原内存表</param>
|
|
|
- /// <param name="condition">过滤条件</param>
|
|
|
- /// <returns>过滤后的内存表</returns>
|
|
|
- public static DataTable FilterDataTable(this DataTable dt, string condition)
|
|
|
- {
|
|
|
- if (condition.Trim().Length == 0)
|
|
|
- {
|
|
|
- return dt;
|
|
|
- }
|
|
|
-
|
|
|
- var newdt = dt.Clone();
|
|
|
- DataRow[] dr = dt.Select(condition);
|
|
|
- dr.ForEach(t => newdt.ImportRow(t));
|
|
|
- return newdt;
|
|
|
- }
|
|
|
}
|
|
|
}
|