c# - Grouping hierarchical data with LINQ -


i have search view needs return grouping of employees based on level in organization hierarchy. user needs have ability group level in hierarchy. example, have drop down check lists allow 1 or more options selected division, department, section, group.

the way have organization structure represented in data model this:

public class organizationentity : ientity {      [datamember]     public int id { get; set; }      [datamember]     public string name { get; set; }      [datamember]     public int organizationentitytypeid { get; set; }      [datamember]     [foreignkey("organizationentitytypeid")]     public virtual organizationentitytype organizationentitytype { get; set; }      [datamember]     public virtual organizationentity parent { get; set; }      public virtual icollection<organizationentity> children { get; set; } } 

the organizationentitytype tells me in organization hierarchy am.

employees linked 1 organization entity employee model looks like:

public class employee : ientity {     [key]     [datamember]             public int id { get; set; }      [required]     [datamember]     public string name { get; set; }      [datamember]     public int cityid { get; set; }      [datamember]     [foreignkey("cityid")]     public virtual city city { get; set; }      [datamember]     public int organizationentityid { get; set; }      [datamember]     [foreignkey("organizationentityid")]     public virtual organizationentity organizationentity { get; set; }  } 

now i'm trying figure out best way group level in org hierarchy using linq. user selects division 1, division 2, sections, group 1 , has grouping set divisions need see data looks this:

divisions employee count
division1 25
division2 3

and if same parameters same except grouping set section data like:

sections employee count
section1 15
section2 3
section3 4

and on other levels.

here sample dataset:

enter image description here

any advice appreciated.

update

added filter employees query include in org level being group , of below , works great! code looks this:

var employees = context.employees.where(o => orgentityids.contains(o.organizationentityid)); 

linq can groupby method, you'll have bit creative way generate grouping key. here's rough idea:

func<employee, string> selector = (n => findorganizationname(n.organizationentity, orgtype)); dictionary<string, int> results = employees.groupby(selector)                                            .todictionary(n => n.key, n => n.count()); 

where findorganizationname recursive function crawls organization hierarchy until finds requested organization type, this:

string findorganizationname(organizationentity entity, int entitytype) {     if (entity == null)     {         return string.empty;     }     else if (entity.organizationentitytypeid == entitytype)     {         return entity.name;     }     else     {         return findorganizationname(entity.parent, entitytype);     } } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -