Search code examples
c#.netsystem.data

Getting DataTables in tree diagram form from a single DataTable


I have a DataTable that contains following columns: type,name,year,version. I want DataTables that contain unique combinations. The table is first divided into multiple tables that have unique name+year combinations. Those tables need to be split again into tables with a unique type. There are a total of 4 types, and 8 different combinations of name + year. So I'm trying to extract a total of 64 Datatables from a single Datatable.

I've tried to first get the distinct combinations of name + year. I've added them to a list(class containing name and Datatable) and if a row from the original table contains that combination i add it to a new DataTable.

DataTable distinctTable = table.DefaultView.ToTable(true, new string[] { name,year });

foreach(var combo in distinctTable) 
{
        complexlist.add(new ComplexList(row[name].ToString() + row[year].ToString()){table = table.Clone()});
}
            foreach (DataRow row in table.Rows)
        {
            foreach(var item in complexlist)
            {

                if(item.Name == row[name].ToString() + row[year].ToString())
                {
                    item.table.Rows.Add(row.ItemArray);
                }
            }
        }

now i have 8 different tables and pretty much have to do the same process again to split by type.

Is there any cleaner and less complex way to do this?


Solution

  • Here's a linq solution. I haven't used DataTables in a long time, usually I convert data to DomainObjects or Models and then work with them before binding to a UI.

    
            class DomainObject
            {
                public string Name { get; set; }
                public int Year { get; set; }
                public string Type { get; set; }
                public string Version { get; set; }
            }
            class GroupedDomainObject
            {
                public string Name { get; set; }
                public int Year { get; set; }
                public string Type { get; set; }
                public IEnumerable<string> Versions { get; set; }
            }
    
            private IEnumerable<GroupedDomainObject> ConvertDataTableToGroupedDomainObject(DataTable dataTable)
            {
                IEnumerable<DomainObject> complexList = dataTable.Select()  // DataTable Select turns it into an IEnumerable
                    .Select(r => new DomainObject  // a linq Select turns it into your DomainObject
                    {
                        Name = r["Name"].ToString(),
                        Year = Convert.ToInt16(r["Year"]),
                        Type = r["Type"].ToString(),
                        Version = r["Version"].ToString()
                    });
    
                // now use linq GroupBy to turn it into (your 64) distinct Groups
                return complexList.GroupBy(i => new { i.Name, i.Year, i.Type }, (key, items) => new GroupedDomainObject
                {
                    Name = key.Name,
                    Year = key.Year,
                    Type = key.Type,
                    Versions = items.Select(o => o.Version)
                });
    
            }
    
            private void testConversionToGroupedDomainObject()
            {
                var mike = new DataTable();
                mike.Columns.Add("Name", typeof(string));
                mike.Columns.Add("Year", typeof(int));
                mike.Columns.Add("Type", typeof(string));
                mike.Columns.Add("Version", typeof(string));
                mike.Rows.Add("NameOne", 2019, "TypeA", "Version1");
                mike.Rows.Add("NameOne", 2018, "TypeB", "Version2");
                mike.Rows.Add("NameOne", 2018, "TypeB", "Version3");
                mike.Rows.Add("NameOne", 2018, "TypeB", "Version4");
                mike.Rows.Add("NameTwo", 2019, "TypeA", "Version1");
                mike.Rows.Add("NameTwo", 2018, "TypeB", "Version2");
                mike.Rows.Add("NameTwo", 2018, "TypeB", "Version3");
                mike.Rows.Add("NameTwo", 2018, "TypeB", "Version4");
    
                var result = ConvertDataTableToGroupedDomainObject(mike);
    
                Debug.Assert(mike.Rows.Count == result.Select(r => r.Versions).Count());
            }