c# - Loop through Dictionary to find dataType if FirstOrDefault is Null -


i have data comes through , generate datatable based on type in dictionary list of string objects. problem i'm having code checks dictionary firstordefault build datatable, if of values null things blow quickly.

is there way can take following code , iterate through rest of dictionary values if particular key.value null within first set of values try , find valid type?

i tried checking , setting type string, if column has real values in fails (for instance datetime when set string). i'd loop through key.key type values in question try , discover if of cells have type , if null string work.

public datatable(ienumerable<dictionary<string, object>> source)     {         if (source != null)         {             var firstitem = source.firstordefault();              if (firstitem != null)             {                 //foreach (var item in source)                 //{                  //    var u = item.values;                 //    var t = item.keys;                  //}                 foreach (var key in firstitem)                 {                     if (key.value == null)  //check if value null , try , find next value key.key isn't                     {                         foreach (var item in source)                         {                             var kk = key.key;                             var ik = item.keys;                          }                          //...some logic try , find if key.key value in question has legitimate value other null set datatype                           //set value type here avoid things blowing up.                         columns.add(new datacolumn() { columnname = key.key, datatype = typeof(string) });                     }                     else                     {                         columns.add(new datacolumn() { columnname = key.key, datatype = key.value.gettype() });                     }                 }                  foreach (var item in source)                 {                     var row = new datarow();                      foreach (var key in item)                     {                         row[key.key] = key.value;                     }                     rows.add(row);                 }             }         }     } 

so, if start getting keys, , pull first non-null value each key, should simplify logic (though may bit more process intensive):

if ( source != null )      {         var keys = ( d in source                           k in d.keys                           select k ).distinct();         foreach ( var key in keys)         {            //...some logic try , find if key.key value in question has legitimate value other null set datatype                  var thiskey = key;            var valuenotnull = source.firstordefault( dictionary => dictionary[thiskey] != null );            var coltype = valuenotnull != null ? valuenotnull[thiskey].gettype() : typeof( string );              dt.columns.add( new datacolumn()            {               columnname = thiskey,               datatype = coltype            } );          } 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -