I am querying an API and populating a C# Datatable with the results, then using a Table Value Parameter to populate a SQL Server table with the results. This is my solution, but it is slow and takes quite some time to process. Is there any optimization that can be done to this syntax to speed up the process? Sorry for the length of code provided, just trying to provide full C# procedure so all transparency is seen.
namespace Test
{
class Program
{
public static string gg;
public static string ff;
public static string ee;
public static string dd;
static void Main(string[] args)
{
DataTable SQLHelperTable = GenerateDataTable();
var response = syncClient.DownloadString(url);
var o = JsonConvert.DeserializeObject<PrimaryObject[]>(response);
foreach (PrimaryObject ro in o)
{
if (ro.dataPull != null)
{
foreach (dataPull data in ro.dataPull)
{
var secondaryData = data.DP.FirstOrDefault();
if (secondaryData != null)
{
gg = secondaryData["gg"];
ff = secondaryData["ff"];
ee = secondaryData["ee"];
dd = secondaryData["dd"];
}
SQLHelperTable.Rows.Add(ro.Alpha, data.a data.b data.c data.d data.e data.f data.g data.h data.i data.j data.k data.l data.m data.n data.o data.p data.q data.r data.s data.t data.u data.v data.w data.x data.y data.z data.aa data.bb, data.cc, gg,ff,ee,dd);
}
}
}
}
public static DataTable GenerateDataTable()
{
DataTable dt = new DataTable();
\\Generate DataTable
return dt;
}
}
public class PrimaryObject
{
public string Alpha { get; set; }
public List<dataPull> dataPull { get; set; }
}
public class dataPull
{
public int a { get; set; }
//Remaining get set statements
public List<Dictionary<string, string>> cc { get; set; }
}
}
1 Answer 1
You can pass all of the properties of your dataPull
class in a single line which will also pass any future properties you decide to add to your object, using Reflection:
var values = data.GetType().GetProperties().Select(p => p.GetValue(data))
.Concat(new object[] {gg, ff, ee, dd});
SQLHelperTable.Rows.Add(ro.Alpha, values);
-
\$\begingroup\$ Will this also handle gg, ff, ee, dd handling null values? \$\endgroup\$user2676140– user26761402017年06月13日 18:40:49 +00:00Commented Jun 13, 2017 at 18:40
-
\$\begingroup\$ What do you mean handle null values? If any of the values is null it will also be added to the row absolutely the same behavior as in your original code \$\endgroup\$Denis– Denis2017年06月13日 20:09:30 +00:00Commented Jun 13, 2017 at 20:09
Explore related questions
See similar questions with these tags.
dataPull
class to theSQLHelperTable.Rows.Add
? \$\endgroup\$gg
,ff
,ee
,dd
orpublic int a { get; set; }
- what kind of names are those? I'm voting to close this quesiton as pseudo/hypothetical code. \$\endgroup\$