Reputation: 107
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!
Upvotes: 0
Views: 74
Reputation: 744
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)
Upvotes: 1