none
Get item.itemsArray in datarow from grouped Datatable RRS feed

  • Question

  • Hello, 
    i want to get item[col] from dataRow but item in dt1.rows return two element item [0] contains the unique ID and item[1] return all attributes i want to add to the row

    var dataSet = ToDataSet(rows.GroupBy(a => a["UniqueID"]).ToDictionary(gdc => gdc.FirstOrDefault()).ToList());
    
                
                DataTable dt1 = dataSet.Tables[0];
    
                foreach (DataRow item in dt1.Rows)
                {
                      row = new Dictionary<string, object>();
                    
                        foreach (DataColumn col in dt.Columns)
                        {
                        
                        row.Add(col.ColumnName, item[col]);
    
                        }
                        rows.Add(row);}

      //item[col] return two element, the first element contains uniqueID and the second element contains all attributes              

    How can i get the item.itemarray[0] ?


                 
    Saturday, February 9, 2019 10:05 AM

All replies

  • I don't know that I follow what you mean by a column in a row has 2 elements. It can't as it can only hold a single value of type object. You can, of course, insert a struct/class as a value but the column itself has a single value. Once you get the column value you can cast it to the type you need and then reference it like you would any other class/struct in .NET.

    Based upon your code it looks like you're trying to convert a group with key and list of values to DataSet. You didn't post the code for ToDataSet but it seems like the key should be your first column and the list of values the second. Personally, just using a list of whatever your group is seems more efficient and easier to work with. Ultimately though your ToDataSet has to be converting the data to a type in order to insert that into the column. Can you post this code?


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, February 11, 2019 3:04 PM
    Moderator