我有一个函数
list<>
可以传入。然后函数
确定列表的类型
是的。我的问题是,我传入一个列表,然后我转换它,但之后当我
将我的列表分配给传入列表
我得到一个错误:
Cannot convert type 'System.Collection.Generic.List<ADMPortal_2.Modles.ProductionPending>' to 'System.Collections.List<T>
代码
数据库调用,返回数据表中的结果,然后将其转换为列表类型。此函数必须能够返回任何类型的列表,如列表、列表等。
string cnnStr = ConfigurationManager.ConnectionStrings["conChdbd1"].ConnectionString;
OracleConnection cnn;
OracleDataReader dr;
public List<T> Execute<T>(string strSql, List<T> list)
{
using (OracleConnection conn = new OracleConnection(cnnStr))
{
using (OracleCommand objCommand = new OracleCommand(strSql, conn))
{
objCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
OracleDataAdapter adp = new OracleDataAdapter(objCommand);
conn.Open();
adp.Fill(dt);
if (dt != null)
{
list = ConvertToList(dt, list).ToList();
}
}
}
return list;
}
错误发生在
分配给列表
public List<T> ConvertToList<T>(DataTable dt, List<T> list)
{
if (list.GetType() == typeof(List<ProductionPending>))
{
list = ConvertToProductionPending(dt, (list as List<ProductionPending>));
}
else if (list.GetType() == typeof(List<ProductionRecent>))
{
list = ConvertToProductionRecent(dt, (list as List<ProductionRecent>));
}
else if (list.GetType() == typeof(List<MirrorDeployments>))
{
list = ConvertToMirror(dt, (list as List<MirrorDeployments>));
}
return list;
}
这里是转换函数
private List<ProductionPending> ConvertToProductionPending(DataTable dt, List<ProductionPending> list)
{
// Convert here
return list;
}
private List<ProductionRecent> ConvertToProductionRecent(DataTable dt, List<ProductionRecent> list)
{
// Convert here
return list;
}
private List<MirrorDeployments> ConvertToMirror(DataTable dt, List<MirrorDeployments> list)
{
// Convert here
return list;
}