将 DataTable 值转换为通用类型
Converting a DataTable value to a Generic Type
我正在尝试将 DataTable 值转换为指定的泛型,但出现错误。
我已经编写了一个类似的功能,效果很好。它 return 是一个字符串,我可以用它来放入 TextBox 或 ComboBox 中,我正在尝试修改它以更改它的 return 类型。这是:
/// <summary>
/// Get Value from a DataTable
/// </summary>
/// <typeparam name="T">Type of Value to get from the DataTable : int | Single | string | double</typeparam>
/// <param name="columnName">Name of the column in the DataTable</param>
/// <param name="table">The DataTable</param>
/// <returns>Get the string value to put in a Textbox</returns>
public static string getDB<T>(string columnName, DataTable table, int index = 0)
{
bool isEmpty = String.IsNullOrEmpty(Convert.ToString(table.Rows[index][columnName]));
return (!isEmpty) ? Convert.ToString(Convert.ChangeType(table.Rows[index][columnName], typeof(T))) : "";
}
我做了这个简单的更改来改变它的 return 类型,但我不确定如何正确转换我的对象,以便将我的 DataTable 转换为通用类型。
public static T getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
return Convert.ChangeType(table.Rows[index][columnName], typeof(T));
}
Error:
Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?)
这个函数在我看来很简单,错误消息也不复杂,我只是缺少一些让我的函数正常工作的东西。
感谢您的帮助,西蒙
这个 T
类型转换模型也对我有用:
public static T getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
return (T) table.Rows[index][columnName];
}
但是,您可以使用Field 方法来转换dataTable 列类型。
Field 提供对指定行中每个列值的强类型访问。
public static T Field<T>(this DataRow row, string columnName)
例如,您可以使用此模型:
foreach (var row in dt.AsEnumerable())
{
users.Add(new User(row.Field<int>("Id")) { Name = row.Field<string>("Name") });
};
我通过在 SO 上搜索更多答案而最终使用的是以下内容:
public static Nullable<T> getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
if (getDB<T>(columnName, table, index) != String.Empty)
return (T)Convert.ChangeType(table.Rows[index][columnName], typeof(T));
return null;
}
通过调用我的原始函数,我能够确定 DataTable 值是否为空且 return 为空(例如,我使用的是 Nullables,因为字符串的默认值与双精度值不同)。如果它不为空,我可以将其转换为通用类型,然后 return 结果。
我正在尝试将 DataTable 值转换为指定的泛型,但出现错误。
我已经编写了一个类似的功能,效果很好。它 return 是一个字符串,我可以用它来放入 TextBox 或 ComboBox 中,我正在尝试修改它以更改它的 return 类型。这是:
/// <summary>
/// Get Value from a DataTable
/// </summary>
/// <typeparam name="T">Type of Value to get from the DataTable : int | Single | string | double</typeparam>
/// <param name="columnName">Name of the column in the DataTable</param>
/// <param name="table">The DataTable</param>
/// <returns>Get the string value to put in a Textbox</returns>
public static string getDB<T>(string columnName, DataTable table, int index = 0)
{
bool isEmpty = String.IsNullOrEmpty(Convert.ToString(table.Rows[index][columnName]));
return (!isEmpty) ? Convert.ToString(Convert.ChangeType(table.Rows[index][columnName], typeof(T))) : "";
}
我做了这个简单的更改来改变它的 return 类型,但我不确定如何正确转换我的对象,以便将我的 DataTable 转换为通用类型。
public static T getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
return Convert.ChangeType(table.Rows[index][columnName], typeof(T));
}
Error:
Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?)
这个函数在我看来很简单,错误消息也不复杂,我只是缺少一些让我的函数正常工作的东西。
感谢您的帮助,西蒙
这个 T
类型转换模型也对我有用:
public static T getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
return (T) table.Rows[index][columnName];
}
但是,您可以使用Field 方法来转换dataTable 列类型。 Field 提供对指定行中每个列值的强类型访问。
public static T Field<T>(this DataRow row, string columnName)
例如,您可以使用此模型:
foreach (var row in dt.AsEnumerable())
{
users.Add(new User(row.Field<int>("Id")) { Name = row.Field<string>("Name") });
};
我通过在 SO 上搜索更多答案而最终使用的是以下内容:
public static Nullable<T> getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
if (getDB<T>(columnName, table, index) != String.Empty)
return (T)Convert.ChangeType(table.Rows[index][columnName], typeof(T));
return null;
}
通过调用我的原始函数,我能够确定 DataTable 值是否为空且 return 为空(例如,我使用的是 Nullables,因为字符串的默认值与双精度值不同)。如果它不为空,我可以将其转换为通用类型,然后 return 结果。