简体   繁体   中英

Dictionary value to Data table c#

Allocation of Race[3]~Brown County,Total:~6866,Allocated~315,Not allocated~6551
Allocation of Age[3]~Brown County,Total:~6866,Allocated~315,Not allocated~6551
Allocation of Race[3]~Boone County,Total:~6866,Allocated~315,Not allocated~6551
Allocation of Age[3]~Boone County,Total:~6866,Allocated~315,Not allocated~6551

Above is my dictionary key value pair.

Key = Allocation of Race[3]~Brown County && Value= Total:~6866,Allocated~315,Not allocated~6551

I am trying to insert these values into a datatable

  table.Columns.Add("Topic");
  table.Columns.Add("County");
  table.Columns.Add("Header");
  table.Columns.Add("Value");

In my key value pair, topic = Allocation of Race[3] && County = Brown County && Header = Total, allocated and Not allocated and value = their respective values.

Initially, I tried to split the key pair using

  string[] Topic_County = key.Split('~');

so Topic_County consists of [0] = Allocation of Race[3] [1]= County name

  foreach (string tc in Topic_County)
            {
                table.Rows.Add(tc);    
            }

when i use foreach loop, allocation of race and county name are coming in the same column How can I add county name under county column, and its header and value in respective positions.

If you use a simple class like this:

public class Datum
{
    public string Topic = "";
    public string County = "";
    public int Allocated = 0;
    public int NotAllocated = 0;

    public int Total()
    {
        return Allocated + NotAllocated;
    }
}

You could use a dictionary still, just use the Topic property and the County property as the key:

        Dictionary<string, Datum> MyData = new Dictionary<string, Datum>();
        Datum info = new Datum
        {
            Topic = "Allocation of Race[3]",
            County = "Brown County",
            Allocated = 315,
            NotAllocated = 6551
        };
        MyData.Add(info.Topic + "-" + info.County, info);

Though a List would probably work just as well and with LINQ you can extract any of the items you need grouped or ordered by whatever criteria you set.

Since Total is a method you don't need to add it to the dictionary just call it as a member of Datum whenever you need the value.

You could add the data to the datatable like this:

        DataTable table = new DataTable();
        table.Columns.Add("Topic");
        table.Columns.Add("County");
        table.Columns.Add("Allocated");
        table.Columns.Add("Not Allocated");
        table.Columns.Add("Total");
        foreach(Datum entry in MyData.Values)
        {
            DataRow NewDataRow = table.NewRow();
            NewDataRow.ItemArray = new string[5]
            {
                entry.Topic,
                entry.County,
                entry.Allocated.ToString(),
                entry.NotAllocated.ToString(),
                entry.Total().ToString()
            };
            table.Rows.Add(NewDataRow);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM