代码之家  ›  专栏  ›  技术社区  ›  Gucci

列出<t>以列出<object>

  •  -3
  • Gucci  · 技术社区  · 7 年前

    我有一个函数 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;
        }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   user8505026user8505026    7 年前

    如果我理解正确,您只需要根据datetable结果集确定必须填充哪个列表。所以,当调用函数why don't add(help)参数时,该参数决定从何处调用该函数,并且取决于此变量,该函数将知道要使用哪个列表。

        2
  •  1
  •   Mihir Dave    7 年前

    应该在方法调用中指定类型。

    这样地

    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);
    }
    
    推荐文章