0

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.

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 .

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!

2
  • 2
    why do you use List<object> Input and not List<Test.Input> Input ? Commented Sep 19, 2018 at 13:39
  • @Tigran , because it may contain null values , so i need to filter them out and also data which i am fetching from DB contains Input Type create object structure. can we do that way? Commented Sep 19, 2018 at 13:49

1 Answer 1

1

You can use a Mapper method:

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Actually the data we are getting from DB is already in object initialisation structure.i think this won't work..will check.Thanks!
That's a bummer! could you post the data structure resulting from the SP to see how it looks like? I'm not sure what you mean by object initialisation structure.
I have posted it in question.data row we have like this: "new Test.Input{ testVar=12,testVar=19}". and we have 100 thousands record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.