列表< T>列出< object> [英] List<T> to List<object>

查看:75
本文介绍了列表< T>列出< object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,可以将任何类型的list<>传递给它.然后,该函数确定列表的类型.我的问题是我传入一个列表,然后将其转换,但是随后当我将列表分配给传入的列表时,我得到一个错误:

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

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

此处分配给列表

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

推荐答案

如果我理解正确,那么您只想确定必须根据日期表结果集填写哪个列表.因此,在调用该函数时,为什么不添加确定从何处调用该函数的(help)-parameter,并且根据此变量,该函数将知道要使用的列表.

If I understand right you just want to determine which list has to be filled up depending on the datetable result set. So, when calling the function why don't add (help)-parameter which determines where it was called from and depending on this variable the function will know what list to use.

这篇关于列表&lt; T&gt;列出&lt; object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆