title: Use two lists of values as parameters in a method and generate objects as many as the length of the two lists C#
body: <p>I have the following two functions:</p>
  <pre class="lang-cs prettyprint-override"><code>        //-----------------------------------FUNCTION 4-----------------------------------
          /// &lt;summary&gt;
          /// Call the specified procedure that will import file1 in SQL Server
          /// &lt;/summary&gt;
          /// &lt;param name=&quot;connectionString&quot;&gt; The connection string to SQL server instance&lt;/param&gt;
          /// &lt;param name=&quot;importedfilepath&quot;&gt; The path of the browsed file&lt;/param&gt;
          public static void LoadLayout(string connectionString, string importedfilepath)
          {
              using (SqlConnection sqlConnection = new SqlConnection(connectionString))
              {
                  sqlConnection.Open();
                  using (var command = sqlConnection.CreateCommand())
                  {
                      command.CommandType = CommandType.StoredProcedure;
                      command.CommandText = &quot;createselectedtableinDB&quot;;
                      command.Parameters.AddWithValue(&quot;@TableName&quot;, importedfilepath);
                      command.ExecuteNonQuery();
                      if (command.ExecuteNonQuery() &gt; 0)
                          MessageBox.Show(&quot;Selected file was loaded successfully&quot;);
                  }
              }
          }
  
          //-----------------------------------FUNCTION 5-----------------------------------
          /// &lt;summary&gt;
          /// Call the specified procedure that will import file2 in SQL Server
          /// &lt;/summary&gt;
          /// &lt;param name=&quot;connectionString&quot;&gt; The connection string to SQL server instance&lt;/param&gt;
          /// &lt;param name=&quot;importedfilepath&quot;&gt; The path of the browsed file&lt;/param&gt;
          public static void LoadBusinessChecks(string connectionString, string importedfilepath)
          {
              using (SqlConnection sqlConnection = new SqlConnection(connectionString))
              {
                  sqlConnection.Open();
                  using (var command = sqlConnection.CreateCommand())
                  {
                      command.CommandType = CommandType.StoredProcedure;
                      command.CommandText = &quot;createselectedtableinDB&quot;;
                      command.Parameters.AddWithValue(&quot;@TableName&quot;, importedfilepath);
                      command.ExecuteNonQuery();
                      if (command.ExecuteNonQuery() &gt; 0)
                          MessageBox.Show(&quot;Selected file was loaded successfully&quot;);
                  }
              }
          }
  </code></pre>
  <p>As you can see the two functions take two parameters (which vary):</p>
  <ul>
  <li>Parameter 1: connection string to SQL server</li>
  <li>Parameter 2: FilePath</li>
  </ul>
  <p>With those 2 parameters I am ok so far. What I want is to further optimize those two functions and basically have 1 function instead of those two. To do so, I know that I need to somehow specify:</p>
  <ul>
  <li>Proceduce name:</li>
  </ul>
  <pre class="lang-cs prettyprint-override"><code>command.CommandText = &quot;createselectedtableinDB&quot;; //can take different values
  </code></pre>
  <ul>
  <li>Procedure parameters and values (2 equal length list)</li>
  </ul>
  <pre class="lang-cs prettyprint-override"><code>command.Parameters.AddWithValue(&quot;@TableName&quot;, importedfilepath); //more than 1 parameter so import a list here
  </code></pre>
  <p>As a result, my expected <em>new mutated function</em> would be like follows:</p>
  <p>Note that the code below it's not correct but I am trying to replicate my intuition so you can understand what I am trying to do. The code below will help you to understand what I am trying to achieve on high level.</p>
  <pre class="lang-cs prettyprint-override"><code>        public static void LoadFiles(string connectionString, string importedfilepath, string ProcedureName, list ParameterName, list ParameterValue)
          {
              using (SqlConnection sqlConnection = new SqlConnection(connectionString))
              {
                  sqlConnection.Open();
                  using (var command = sqlConnection.CreateCommand())
                  {
                      command.CommandType = CommandType.StoredProcedure;
                      command.CommandText = ProcedureName;
                      
                      foreach (string parametername in ParameterName)
                      {
                          foreach (string parametervalue in ParameterValue) 
                      {
                              command.Parameters.AddWithValue(parametername, parametervalue ); //Generate as many as the length of the two lists
                      }
                      }
                      command.ExecuteNonQuery();
                      if (command.ExecuteNonQuery() &gt; 0)
                          MessageBox.Show(&quot;Selected file was loaded successfully&quot;);
                  }
              }
          }
  </code></pre>
  <p>When calling the method from a LoadButton1_Click</p>
  <pre class="lang-cs prettyprint-override"><code>        private void LoadButton1_Click(object sender, RoutedEventArgs e)
          {   
              ParameterNamelist = list [&quot;@TableName&quot;, &quot;@Username&quot;, &quot;@Email&quot;]
              ParameterValuelist = list [importedfilepath, &quot;Nikos&quot;, &quot;nikos@stackoverflow.com&quot;]
  
              var connectionString = SQLServerConnectionDetails();
              LoadFiles(connectionString, FileNameTextBox.Text, &quot;SelectUsernameProcedure&quot;, ParameterNamelist, ParameterValuelist);
          }
  </code></pre>
  <p>When calling the exact method from a LoadButton2_Click</p>
  <pre class="lang-cs prettyprint-override"><code>        private void LoadButton2_Click(object sender, RoutedEventArgs e)
          {   
              ParameterNamelist = list [&quot;@TableName&quot;, &quot;@ProductName&quot;]
              ParameterValuelist = list [importedfilepath, &quot;Choco&quot;]
  
              var connectionString = SQLServerConnectionDetails();
              LoadFiles(connectionString, FileNameTextBox.Text, &quot;SelectProductProcedure&quot;, ParameterNamelist, ParameterValuelist);
          }
  </code></pre>