在这种情况下,我有一个dtData
字符串的 DataTable。此 DataTable 仅包含 1 列。在该列中,我们有日期,它是字符串形式的。我想将此列转换为日期类型并将其存储在列表中。
问问题
2404 次
2 回答
1
我认为循环将是最简单的:
CultureInfo enUS = new CultureInfo("en-US");
foreach(var r in dt.Rows)
if(DateTime.TryParseExact((string)r[0], "DATE FORMAT HERE", enUS, DateTimeStyles.None, out var d)
list.Add(d);
也可以用LINQ,但是感觉比较乱
CultureInfo enUS = new CultureInfo("en-US");
dt.AsQueryable()
.Select(r => DateTime.TryParseExact((string)r[0], "DATE FORMAT HERE", enUS, DateTimeStyles.None, out var d) ? d : DateTime.MinValue)
.Where(d => d > DateTime.MinValue)
.ToList();
于 2020-04-30T07:31:46.130 回答
0
public static class DBHelper<T>
{
public static void ConvertDataRowToModel(T modelObj, DataRow dataRow)
{
var properties = from prop in modelObj.GetType().GetProperties()
select prop;
foreach (var property in properties)
{
dynamic propertyValue = null;
foreach (DataColumn col in dataRow.Table.Columns)
{
if (property.Name.ToUpper().Equals(col.ColumnName.ToString().ToUpper()))
{
propertyValue = dataRow[col.ColumnName] != DBNull.Value ? dataRow[col.ColumnName] : null;
break;
}
}
if (propertyValue != null)
{
var propertyType = property.PropertyType.FullName;
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = property.PropertyType.GetGenericArguments()[0].FullName;
}
switch (propertyType)
{
case "System.Int32":
{
property.SetValue(modelObj, Convert.ToInt32(propertyValue), null);
break;
}
case "System.Nullable[System.Int32]":
{
property.SetValue(modelObj, Convert.ToInt32(propertyValue), null);
break;
}
case "System.Int64":
{
property.SetValue(modelObj, Convert.ToInt64(propertyValue), null);
break;
}
case "System.Boolean":
{
property.SetValue(modelObj, Convert.ToBoolean(Convert.ToInt16(propertyValue)), null);
break;
}
case "System.String":
{
var val = WebUtility.HtmlDecode(Convert.ToString(propertyValue));
property.SetValue(modelObj, val, null);
break;
}
case "System.DateTime":
{
property.SetValue(modelObj, Convert.ToDateTime(propertyValue), null);
break;
}
case "System.Decimal":
{
property.SetValue(modelObj, Math.Round(Convert.ToDecimal(propertyValue), 2), null);
break;
}
case "System.Double":
{
property.SetValue(modelObj, Math.Round(Convert.ToDouble(propertyValue), 2), null);
break;
}
case "System.Byte[]":
{
property.SetValue(modelObj, (Byte[])(propertyValue), null);
break;
}
default:
{
Type t = Enum.GetUnderlyingType(property.PropertyType);
switch (t.FullName)
{
case "System.Int16":
{
property.SetValue(modelObj, Convert.ToInt16(propertyValue), null);
break;
}
case "System.Int32":
{
property.SetValue(modelObj, Convert.ToInt32(propertyValue), null);
break;
}
}
break;
}
}
}
}
}
public static List<T> ConvertDataTableToModelList(DataTable dataTable)
{
List<T> modelList = new List<T>();
foreach (DataRow dataRow in dataTable.Rows)
{ var ob = Activator.CreateInstance<T>();
ConvertDataRowToModel(ob, dataRow);
modelList.Add(ob);
}
return modelList;
}
}
上面的类具有将数据表转换为列表的通用方法。调用机制:Lstsample lstsample==DBHelper.ConvertDataTableToModelList(datasetsample.Tables[0]);
于 2020-04-30T07:39:33.103 回答