简体   繁体   English

如何在C#中使用具有相似类型的对象列表来创建类型数组

[英]How to create array of type with having list of object similar type in C#

We have third party class which only accepts arrays of object. 我们有第三方类,仅接受对象数组。

Third Party class : 第三方类别:

public class Test
{
    public class Input
    {

        public int testVar { get; set; }
        public int testVar2 { get; set; }
    }
    //some methods

    public static List<someType> Convert(Input[] data)
    {
        //execute some steps 
    }
}

in DB, we have the data column which we are interested and it has thousand of records. 在DB中,我们有一个我们感兴趣的data列,它具有数千条记录。

Id        data

    1   new Test.Input{ testVar=12,testVar=19}
    2   new Test.Input{ testVar=16,testVar=12}
    3   new Test.Input{ testVar=26,testVar=11}
    -

i am trying to create a class and invoke Convert method of Test class by providing array of Input type object . 我试图通过提供Input类型对象的数组来创建一个类并调用Test类的Convert方法。

public class ImplementTest
{
    public void CallConvert()
    {
        // get the data from DB in list
        List<object> Input = new List<object>();
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "dbo.ReadAll_Input";

                con.Open();

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        Input.Add(dr["data"]);
                    }
                }
            }
        }

        //convert list object to input type object  
        var inputs = new Test.Input[]
        {
           //how to pass Input list efficiently
        };

        var output = Test.Convert(inputs).ToArray();
    }
}

can anyone help me on passing Input list object to Create array of object efficiently please? 有人可以帮助我将输入列表对象传递给有效地创建对象数组吗?

Thanks! 谢谢!

You can use a Mapper method: 您可以使用Mapper方法:

public Input MapRow(IDataRecord row)
{
    return new Input
    {
        Name = row["Name"].ToString(),
        Number = int.Parse(row["Number"].ToString())
    };
}

And use it like this: 并像这样使用它:

public void CallConvert()
{
    // get the data from DB in list
    List<Input> inputs = new List<Input>();

    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.ReadAll_Input";

            con.Open();

            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    inputs.Add(MapRow(dr));
                }
            }
        }
    }

    var output=Test.Convert(inputs.ToArray());
}

And to make it work, your stored procedure should return a table of inputs with, in this case, two columns (Name, Number) 为了使其工作,存储过程应返回一个输入表,在这种情况下,该表具有两列(Name, Number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM