Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

c# - List<T> to List<object>

I have a function where any type of list<> can be passed into. The function then determines which type the list is. My problem is that Im passing in a list then I convert it but afterwards when I assign my list to the passed in list I get an error:

Cannot convert type 'System.Collection.Generic.List<ADMPortal_2.Modles.ProductionPending>' to 'System.Collections.List<T>

CODE

Database call which returns the results in a datatable which is then to be converted to its list type. This function must be able to return any type of list e.g. List, List etc.

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;
    }

Here the error happens when assigning to 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;
    }

Here the convert functions

    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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should specify your type in the method call.

like this

objCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
OracleDataAdapter adp = new OracleDataAdapter(objCommand);
conn.Open();
adp.Fill(dt);
if (dt != null)
{
    //Here i cahnged the code
    list = ConvertToList<T>(dt, list);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...