我有一个方法可以将数据表从 LINQ 返回到 SQL
IE
public static DataTable LinqToDataTable<T>(IEnumerable<T> source)
{
DataTable output = new DataTable();
try
{
PropertyInfo[] properties = typeof(T).GetProperties();
foreach (var prop in properties)
{
output.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
prop.PropertyType) ?? prop.PropertyType);
}
foreach (var item in source)
{
DataRow row = output.NewRow();
foreach (var prop in properties)
{
row[prop.Name] = prop.GetValue(item, null) ?? DBNull.Value;
}
output.Rows.Add(row);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return output;
}
我的问题是,如果任何 DateTime 字段为空,则此方法会引发异常
The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value
类型。
如何纠正错误。请任何人帮助我