C# 将DataTable值转换为泛型类型

C# 将DataTable值转换为泛型类型,c#,generics,datatable,C#,Generics,Datatable,我试图将DataTable值转换为指定的泛型类型,但出现错误 我已经编写了一个类似的功能,它工作得很好。它返回一个字符串,我可以用它放在文本框或组合框中,我正试图修改它以改变它的返回类型。这是: /// <summary> /// Get Value from a DataTable /// </summary> /// <typeparam name="T">Type of Value to get from the DataTable : int | Si

我试图将DataTable值转换为指定的泛型类型,但出现错误

我已经编写了一个类似的功能,它工作得很好。它返回一个字符串,我可以用它放在文本框或组合框中,我正试图修改它以改变它的返回类型。这是:

/// <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))) : "";
}
//
///从数据表中获取值
/// 
///要从数据表获取的值类型:int | Single | string | double
///数据表中列的名称
///数据表
///获取要放入文本框中的字符串值
公共静态字符串getDB(字符串列名,数据表,int索引=0)
{
bool isEmpty=String.IsNullOrEmpty(Convert.ToString(table.Rows[index][columnName]);
return(!isEmpty)?Convert.ToString(Convert.ChangeType(table.Rows[index][columnName],typeof(T)):“”;
}
我做了这个简单的更改来改变它的返回类型,但我不确定如何正确地强制转换我的对象,以便将我的DataTable转换为泛型类型

public static T getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
    return Convert.ChangeType(table.Rows[index][columnName], typeof(T));
}
public static T getDBSpecifiedType(字符串列名,数据表,int索引=0)
{
返回Convert.ChangeType(table.Rows[index][columnName],typeof(T));
}
错误:
无法将类型“object”隐式转换为“T”。存在显式转换(是否缺少强制转换?)

在我看来,这个函数很简单,错误消息并不复杂,我只是缺少了一些让我的函数工作的东西


感谢您的帮助,Simon

这款
T
型铸造也适合我:

public static T getDBSpecifiedType<T>(string columnName, DataTable table, int index = 0)
{
     return (T) table.Rows[index][columnName];
}
public static T getDBSpecifiedType(字符串列名,数据表,int索引=0)
{
return(T)table.Rows[index][columnName];
}
但是,您可以使用方法转换dataTable列类型。 提供对指定行中每个列值的强类型访问

public static T Field<T>(this DataRow row,  string columnName)
公共静态T字段(此数据行,字符串列名)
例如,您可以使用该模型:

foreach (var row in dt.AsEnumerable())                        
{
    users.Add(new User(row.Field<int>("Id")) { Name = row.Field<string>("Name") });       
};
foreach(dt.AsEnumerable()中的变量行)
{
添加(新用户(row.Field(“Id”){Name=row.Field(“Name”)});
};

我在上搜索了更多答案,结果如下:

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;
}
public静态可空getDBSpecifiedType(字符串列名,数据表,int索引=0)
{
if(getDB(columnName,table,index)!=String.Empty)
return(T)Convert.ChangeType(table.Rows[index][columnName],typeof(T));
返回null;
}

通过调用我的原始函数,我能够确定DataTable值是否为空并返回null(例如,我使用null值,因为字符串的默认值与double值不同)。如果不是空的,我可以将其转换为泛型类型并返回结果。

错误消息是什么?可能您没有首先检查索引和/或列名是否存在?错误在getDBSpecifiedType函数中:“无法将类型“object”隐式转换为“t”。存在显式转换(是否缺少强制转换?)