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; }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Do you pass all of the properties from dataPull class to the SQLHelperTable.Rows.Add? \$\endgroup\$ – Denis Jun 13 '17 at 14:01
  • \$\begingroup\$ yes, all are passed in order to populate the DataTable \$\endgroup\$ – user2676140 Jun 13 '17 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\$ – t3chb0t Jun 13 '17 at 18:17
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);
\$\endgroup\$
  • \$\begingroup\$ Will this also handle gg, ff, ee, dd handling null values? \$\endgroup\$ – user2676140 Jun 13 '17 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 Jun 13 '17 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.