1
\$\begingroup\$

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; }
 }
}
t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Jun 13, 2017 at 13:28
\$\endgroup\$
3
  • \$\begingroup\$ Do you pass all of the properties from dataPull class to the SQLHelperTable.Rows.Add? \$\endgroup\$ Commented Jun 13, 2017 at 14:01
  • \$\begingroup\$ yes, all are passed in order to populate the DataTable \$\endgroup\$ Commented Jun 13, 2017 at 15:49
  • 1
    \$\begingroup\$ gg, ff, ee, dd or public int a { get; set; } - what kind of names are those? I'm voting to close this quesiton as pseudo/hypothetical code. \$\endgroup\$ Commented Jun 13, 2017 at 18:17

1 Answer 1

1
\$\begingroup\$

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);
answered Jun 13, 2017 at 16:23
\$\endgroup\$
2
  • \$\begingroup\$ Will this also handle gg, ff, ee, dd handling null values? \$\endgroup\$ Commented 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\$ Commented Jun 13, 2017 at 20:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.