I am trying to dynamically restructure some data that will be displayed in a tree structure, which will allow the user to select up to three of the following dimensions to group the data:
Organisation
Company
Site
Division
Department
So, for example, if the user had to choose what they would like to group by company, then the site then the section ... the following code will perform the necessary groupings.
var entities = orgEntities
.GroupBy(o => new { o.CompanyID, o.CompanyName })
.Select(grp1 => new TreeViewItem
{
CompanyID = grp1.Key.CompanyID,
DisplayName = grp1.Key.CompanyName,
ItemTypeEnum = TreeViewItemType.Company,
SubItems = grp1
.GroupBy(o => new { o.SiteID, o.SiteName })
.Select(grp2 => new TreeViewItem
{
SiteID = grp2.Key.SiteID,
DisplayName = grp2.Key.SiteName,
ItemTypeEnum = TreeViewItemType.Site,
SubItems = grp2
.GroupBy(o => new { o.Division })
.Select(grp3 => new TreeViewItem
{
DisplayName = grp3.Key.Division,
ItemTypeEnum = TreeViewItemType.Division,
}).ToList()
}).ToList()
})
.ToList();
This will give a structure like this:
+ Company A
+ Site A
+ Division 1
+ Division 2
+ Site B
+ Division 1
+ Company B
+ Site C
+ Division 2
+ Company C
+ Site D
However, this only gives me a large number of combinations.
How would I decide to convert this to something that could create an equivalent expression dynamically based on three dimensions selected by the user, and therefore I do not need to create one of these expressions for each combination ??
Thanks guys.
source
share