0

I try to add a string[] to a List

Code:

foreach (int software_id in softwarelist)
{
    String sqlstring = "SELECT SoftwareName, Task_idn_Install, Task_idn_Deinstall, KZ, ParameterFeld FROM TUI_Software WHERE Sw_idn = '" + software_id + "'";
    SqlCommand cmd1 = new SqlCommand(sqlstring, con);
    SqlDataReader result_sw_info = cmd1.ExecuteReader();

    if (result_sw_info.HasRows)
    {
        result_sw_info.Read();

        softwarename = result_sw_info[0].ToString();
        task_idn_install = result_sw_info[1].ToString();
        task_idn_deinstall = result_sw_info[2].ToString();
        indicator = result_sw_info[3].ToString();
        parameter = result_sw_info[4].ToString();

        string[] software_data = new string[] {sw_id, softwarename, task_idn_install, task_idn_deinstall, indicator, parameter };

        softwarelist_insert.Add(software_data);
    }
    result_sw_info.Close();
}

So the keys in the softwarelist_insert begins with 0. But how can I add the software_data with the key software_id in the foreach. Cause I want to identifier the arrys in the list with the software_id.

ekad
  • 14,436
  • 26
  • 44
  • 46
mnlfischer
  • 397
  • 4
  • 12
  • 26

2 Answers2

2

Use a System.Collections.Generic Dictionary<string, string[]> instead of a List<string[]>.

var softwarelist_insert = new Dictionary<string, string[]>();
...
softwarelist_insert.Add(software_id, software_data);

Better still, use a strongly typed object instead of a string[].

public class SoftwareData {
  public string SoftwareName { get; set; }
  public string TaskIdnInstall { get; set; }
  ...
}

and then

var softwarelist_insert = new Dictionary<string, SoftwareData>();
...
var = new SoftwareData {
  SoftwareName = result_sw_info[0].ToString(),
  TaskIdnInstall = result_sw_info[1].ToString(),
}
softwarelist_insert.Add(software_id, data);
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
0

You can create your own type and add the data to it. First create a class like this

public class softwarelist_insert_class
{
  public string ID {get;set;}
  public List<string>softwarelist_insert{get;set;}
}

Then you can create a list variable of this type

List<softwarelist_insert_class> variable_name=new List<softwarelist_insert_class>();

Then you can add data to it like:

List<string> software_data = List<string> {sw_id, softwarename, task_idn_install, task_idn_deinstall, indicator, parameter };

variable_name.Add({id,software_data});
souvickcse
  • 7,742
  • 5
  • 37
  • 64