c# - Group by Items in list of objects (Sitecore Item) and bind with Repeater -


sorry long description want make simple users understand problem. have list contains departmentitem , employeeitem.

 var empsubrellist = new list<empdeptrel>();  internal class empdeptrel {     public item departmentitem { get; set; }     public item employeeitem { get; set; } } 

here code explains how adding items list:

item userprofile = sitecore.context.database.getitem("/sitecore/content/intranet/user profiles");  foreach (var subrelitem in userprofile.axes.getdescendants().where(p => p.templateid.tostring() == settings.getsetting("subreltemplateid")                                                   && p["subsidiary"] == subssdiaryitem.id.tostring()).orderby(p => p["department"])) {     empsubrellist.add(new empdeptrel     {          departmentitem = getitembyid(subrelitem["department"]),          employeeitem = subrelitem.parent.parent     });        } 

i bind repeater:

repemployees.datasource = empsubrellist; repemployees.databind();  <asp:repeater runat="server" id="repemployees">    <itemtemplate>        <li>             <%# name(eval("departmentitem") item)%>             <%# name(eval("employeeitem") item)%>                  </li>    </itemtemplate> 

here code method "name()" inside repeater

protected string name(item item) {     if (item != null)     {         return item.name;     }     return string.empty; } 

the output is:

it testuser1 administration testuser2 administration testuser3 administration testuser4 administration testuser5 finance testuser6 

is somehow possible group list "empsubrellist" department can following output:

it testuser1 administration testuser2 testuser3 testuser4 testuser5 finance testuser6 

as serv wrote, can grouping before setting datasource of repeater:

ienumerable<keyvaluepair<string, string>> datasource = empsubrellist     .groupby(listitem => name(listitem.departmentitem))     .select(grouping => new keyvaluepair<string, string>(         groupping.key,         string.join(", ", grouping.select(emp => name(emp.employeeitem)))     ));  repemployees.datasource = datasource; 

and in html code:

<asp:repeater runat="server" id="repemployees">    <itemtemplate>        <li>             <%# ((keyvaluepair<string, string>)container.dataitem).key %>             <%# ((keyvaluepair<string, string>)container.dataitem).value %>        </li>    </itemtemplate> </asp:repeater> 

Comments