0

I have data like

 ConcurrentDictionary<string, ConcurrentDictionary<string,string> OneTwoThree =
                new ConcurrentDictionary<string, ConcurrentDictionary<string, string>();

I want a result like this Final Image What I tried:

 DataTable dt = new DataTable();
 dt.Columns.Add("TwoDetails");
 ISet<string> two = new HashSet<string>();
 Parallel.ForEach(OneTwoThree , One=>
             {
                 dt.Columns.Add(One.Key);
                 foreach(var Two in One.Value)
                 { 
                  two.Add(Two.Key); // To get Distinct Values
                 }
             });
 foreach(var item in two)
 { 
   var row = dt.NewRow();
   row["TwoDetails"] = row;
 }

Now I don't have the idea to append "Three-Values" to a particular cell, as shown in the image.

Any Suggestions.

Naira
  • 55
  • 1
  • 1
  • 9
  • @TimSchmelter.. if rows name and columns name are there, How can u insert data at cell(which is intersection of Row name and Column name). – Naira Mar 22 '18 at 13:29
  • Possible to dublicate https://stackoverflow.com/questions/12292287/adding-values-to-specific-datatable-cells – sachin Mar 22 '18 at 13:39
  • @sachin .. here i am dealing with rownames, which are sorted while creating new row in datatable. – Naira Mar 22 '18 at 13:49
  • any suggestions? – Naira Mar 22 '18 at 13:59

2 Answers2

2

Another pivot table question. Done a 1000. See code below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication31
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, Dictionary<string, string>> OneTwoThree = new Dictionary<string, Dictionary<string, string>>() {
                {"A", new Dictionary<string,string>(){{"U","s"}, {"Z","a"}}},
                {"B", new Dictionary<string,string>(){{"W","e"},{"X","d"},{"Y","d"}}},
                {"C", new Dictionary<string,string>(){{"V","f"}, {"W","a"},{"Z","w"}}},
            };

            string[] columns = OneTwoThree.Select(x => x.Key).OrderBy(x => x).ToArray();

            DataTable dt = new DataTable();
            dt.Columns.Add("TwoDetails", typeof(string));
            foreach(string column in columns)
            {
                dt.Columns.Add(column, typeof(string));
            }

            string[] rows = OneTwoThree.Select(x => x.Value.Select(y => y.Key)).SelectMany(x => x).Distinct().OrderBy(x => x).ToArray();
            var flip = rows.Select(x => new { row = x, columns = OneTwoThree.Where(y => y.Value.ContainsKey(x)).Select(y => new { col = y.Key, value = y.Value[x] }).ToList() }).ToList();
            //create pivot table
            foreach (var row in flip)
            {
                DataRow newRow = dt.Rows.Add();
                newRow["TwoDetails"] = row.row;
                foreach (var column in row.columns)
                {
                    newRow[column.col] = column.value;
                }
            }
        }

    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • nice one .ty.but in your OneTwoThree dictionary should contains only 3 keys(i.e A,B,C) and inner dictionary should contains U,V,W,X,Y,Z(according to problem statement, see image).Plz edit. however i got the idea to solve by ur approch. Ty – Naira Mar 22 '18 at 16:10
0

You can not access the data table rows with their names but using row numbers you can find a particular row in the data table.So for that, You need to create one class like

 public class DataTableDetails
{
    public string RowName { get; set; }
    public int RowNumber { get; set; }
}

And this is not effecient solution, but can help you

    ISet<string> two = new HashSet<string>();
    List<DataTableDetails> dtdetaills=new List<DataTableDetails>();
     Parallel.ForEach(OneTwoThree , One=>
                 {
                     dt.Columns.Add(One.Key);
                     foreach(var Two in One.Value)
                     { 
                      two.Add(Two.Key); // To get Distinct Values
                     }
                 });
int count=0;
foreach(var item in two)
 { 
   var row = dt.NewRow();
   row["TwoDetails"] = row;
   DataTableDetails details = new DataTableDetails();
   details.RowName = item;
   details.RowNumber = count++; // we can easily get row number 
   dtdetails.Add(details);
 }

And Finally

foreach(var One in OnrTwoThree)
            {
                foreach(var Two in One.Value)
                {
                   foreach(var rowdetails in dtdetails)
                    {
                        if(Two.Key==rowdetails.RowName)
                        {
                            dt.Rows[rowdetails.RowNumber][One.Key] = Two.Value;
                        }

                    }
                }
            }
Mahender Kvs
  • 174
  • 2
  • 7