您可以使其通用,并初始化
DataColumn
具有
typeof(T)
:
public static DataTable CreateAttributeValueDataTable<T>(AttributeValueList attributeValueList)
{
var dataTable = new DataTable();
dataTable.Columns.Add("attr_id", typeof(Int32));
dataTable.Columns.Add("attr_value", typeof(T));
dataTable.Columns.Add("effective_date", typeof(DateTime));
dataTable.Columns.Add("expiration_date", typeof(DateTime));
dataTable.Columns.Add("last_updated_user", typeof(string));
dataTable.Columns.Add("last_updated_date_time", typeof(DateTime));
if (attributeValueList.Count > 0)
{
foreach (var item in attributeValueList)
{
DataRow dr = dataTable.NewRow();
dr[0] = item.AttributeId;
dr[1] = item.Value;
dr[2] = item.EffectiveDate;
dr[3] = item.ExpirationDate;
dr[4] = item.LastUpdatedUser;
dr[5] = item.LastUpdatedDate;
dataTable.Rows.Add(dr);
}
}
return dataTable;
}
现在您可以这样称呼它:
public class AttributeValueInsertDate : EntityBase
{
public static object ResolveRequest(AttributeValueList attributeValueList)
{
return new
{
dateValuesTVP = AttributeValue.CreateAttributeValueDataTable<DateTime>(attributeValueList)
};
}
}
public class AttributeValueInsertDecimal : EntityBase
{
public static object ResolveRequest(AttributeValueList attributeValueList)
{
return new
{
decimalValuesTVP = AttributeValue.CreateAttributeValueDataTable<Decimal>(attributeValueList)
};
}
}
public class AttributeValueInsertText
{
public static object ResolveRequest(AttributeValueList attributeValueList)
{
return new
{
textValuesTVP = AttributeValue.CreateAttributeValueDataTable<string>(attributeValueList)
};
}
}