Browse Source

datatable和list转换报错

SCP_CYJ
qian 4 months ago
parent
commit
f92c866451
  1. 53
      UniApiGroup/Controller/OdbcApiQadController.cs

53
UniApiGroup/Controller/OdbcApiQadController.cs

@ -31,6 +31,8 @@ using static CK.SCP.UniApi.Controller.NetPriceController;
using System.Xml.Linq; using System.Xml.Linq;
using System.Security.Principal; using System.Security.Principal;
using System.Diagnostics; using System.Diagnostics;
using Z.BulkOperations.Internal.InformationSchema;
using System.Collections;
namespace CK.SCP.GrupUniApi.Controller namespace CK.SCP.GrupUniApi.Controller
{ {
@ -1648,8 +1650,8 @@ namespace CK.SCP.GrupUniApi.Controller
hisList = idb.xxDiMaxes_Order.Where(q => q.CREATEDDATE > time).ToList(); hisList = idb.xxDiMaxes_Order.Where(q => q.CREATEDDATE > time).ToList();
// 读取一年内 所有审批完的一般材料订单 // 读取一年内 所有审批完的一般材料订单
DataTable b = GetGeneralMaterialOrder(); DataTable b = GetGeneralMaterialOrder();
List<GeneralMaterialOrder> orderlist = ChangKeTec.Utils.ListHelper.DataTableToList<GeneralMaterialOrder>(b); List<GeneralMaterialOrder> orderlist = GetTableToList<GeneralMaterialOrder>(b);
List<xxDiMaxes_Order>maxes_Orders = ChangKeTec.Utils.ListHelper.DataTableToList<xxDiMaxes_Order>(b); List<xxDiMaxes_Order>maxes_Orders = GetTableToList<xxDiMaxes_Order>(b);
// 取历史表不存在的数据 // 取历史表不存在的数据
var expectedList = maxes_Orders.Where(x=> !hisList.Any(e=>e.DI_BATCHNO == x.DI_BATCHNO && e.PURDOCNO == x.PURDOCNO && e.PURDOCITEMNO ==x.PURDOCITEMNO)).ToList(); var expectedList = maxes_Orders.Where(x=> !hisList.Any(e=>e.DI_BATCHNO == x.DI_BATCHNO && e.PURDOCNO == x.PURDOCNO && e.PURDOCITEMNO ==x.PURDOCITEMNO)).ToList();
if (expectedList.Count() == 0) if (expectedList.Count() == 0)
@ -1917,5 +1919,52 @@ namespace CK.SCP.GrupUniApi.Controller
} }
} }
} }
/// <summary>
/// ת»»
/// </summary>
/// <param name="list">¼¯ºÏ</param>
/// <returns></returns>
public static List<T> GetTableToList<T>(this DataTable dt)
{
var list = new List<T>();
var plist = new List<PropertyInfo>(typeof(T).GetProperties());
foreach (DataRow item in dt.Rows)
{
T s = Activator.CreateInstance<T>();
for (int i = 0; i < dt.Columns.Count; i++)
{
PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
if (info != null)
{
try
{
if (!Convert.IsDBNull(item[i]))
{
object v = null;
if (info.PropertyType.ToString().Contains("System.Nullable"))
{
v = Convert.ChangeType(item[i], Nullable.GetUnderlyingType(info.PropertyType));
}
else
{
v = Convert.ChangeType(item[i], info.PropertyType);
}
info.SetValue(s, v, null);
}
}
catch (Exception ex)
{
throw new Exception("×Ö¶Î[" + info.Name + "]ת»»³ö´í," + ex.Message);
}
}
}
list.Add(s);
}
return list;
}
} }
} }

Loading…
Cancel
Save