C# Linq data structure, for eventual data binding. Best way to do? -


i have set of "parts". each part has name , set of attributes. attribute set of string keys , values. example:

part 1     width   200     depth   400     voltage 240v 

i'm storing parts , attributes in dictionary this:

dictionary<string, dictionary<string, string>> parts; 

so can see if part exists parts.containskey(part) , if does, can see if attribute exists parts[part].containskey(attribute). far, banal.

now want either data bind structure, or otherwise generate set of rows, each column attribute. not parts have attributes, there "nulls" in places. have access list of attributes found in entire set, list (there 59 possible attributes in actual production system).

my code generating set of rows, assuming first column part name, rather clunky code below. results in list of lists of string. 1 list of string each part (one "row" each part).

i'm pretty sure there's simple 1 liner linq statement this. i'm hoping can use data-bind list or data grid display @ point. can me out it?

i provide full repro below (add new c# console project), example data.

using system; using system.collections.generic;  namespace consoleapplication1 {     class program     {         static void main(string[] args)         {                           // set of parts.              dictionary<string, dictionary<string, string>> hashparttoattributes = new dictionary<string,dictionary<string,string>>();              hashparttoattributes.add("part 1", new dictionary<string,string>());             hashparttoattributes.add("part 2", new dictionary<string,string>());             hashparttoattributes.add("part 3", new dictionary<string,string>());             hashparttoattributes.add("part 4", new dictionary<string,string>());              // add in attributes of parts.              {                 hashparttoattributes["part 1"].add("width", "200");                 hashparttoattributes["part 1"].add("height", "400");                 hashparttoattributes["part 1"].add("depth", "600");                  hashparttoattributes["part 2"].add("width", "300");                 hashparttoattributes["part 2"].add("height", "700");                 hashparttoattributes["part 2"].add("depth", "100");                 hashparttoattributes["part 2"].add("voltage", "240v");                  hashparttoattributes["part 3"].add("voltage", "110v");                 hashparttoattributes["part 3"].add("bandwidth", "25");                 hashparttoattributes["part 3"].add("frequency", "5");                 hashparttoattributes["part 3"].add("height", "900");                  hashparttoattributes["part 4"].add("width", "150");                 hashparttoattributes["part 4"].add("height", "740");                 hashparttoattributes["part 4"].add("depth", "920");                 hashparttoattributes["part 4"].add("voltage", "240v");                 hashparttoattributes["part 4"].add("bandwidth", "40");                 hashparttoattributes["part 4"].add("frequency", "5");             }              // complete set of attributes (column headings)              list<string> attributekeys = new list<string>() {                 "width", "height", "depth", "voltage", "bandwidth", "frequency"             };              // construct row each part.              list<list<string>> rows = new list<list<string>>();              foreach (string part in hashparttoattributes.keys)             {                 list<string> row = new list<string>() { part };                  foreach (string key in attributekeys)                 {                     dictionary<string, string> attributes = hashparttoattributes[part];                      if (attributes != null && attributes.containskey(key))                     {                         row.add(attributes[key]);                     }                     else                     {                         row.add(null);                     }                 }                  rows.add(row);             }                         // print out headings.              console.write("{0, -10}", "part");              foreach (string heading in attributekeys)             {                 console.write("{0, -10}", heading);             }              console.writeline();             console.writeline();              // print out rows              foreach (list<string> row in rows)             {                 foreach (string item in row)                 {                     if (item != null)                     {                         console.write("{0, -10}", item);                     }                     else                     {                         console.write("{0, -10}", "-");                     }                 }                  console.writeline();             }         }     } } 

a trivial linq statement doesn't exist, can use following:

hashparttoattributes.select(     p => new [] { p.key }.concat(         attributekeys.groupjoin(p.value, ak => ak, => a.key,             (ak, a) => a.select(x => x.value).singleordefault())                      .defaultifempty())                          .toarray()                            ) 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -