0

Sorry for the long description but I want to make it simple for the users to understand my problem. I have a list that contains DepartmentItem and EmployeeItem.

 var empSubRelList = new List<EmpDeptRel>();

internal class EmpDeptRel
{
    public Item DepartmentItem { get; set; }
    public Item EmployeeItem { get; set; }
}

Here is the code that explains how I am adding items to the 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 it with a repeater:

repEmployees.DataSource = empSubRelList;
repEmployees.DataBind();

<asp:Repeater runat="server" ID="repEmployees">
   <ItemTemplate>
       <li>
            <%# Name(Eval("DepartmentItem") as Item)%>
            <%# Name(Eval("EmployeeItem") as Item)%>          
       </li>
   </ItemTemplate>

Here is the code for the method "Name()" which is inside the 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 it somehow possible to group the list "empSubRelList" With Department So that I can get following output:

IT TestUser1
Administration TestUser2 TestUser3 TestUser4 TestUser5
Finance TestUser6
Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
Kamran
  • 4,010
  • 14
  • 60
  • 112
  • I think that can help you http://stackoverflow.com/questions/20637654/how-to-retrieve-common-items-from-list-c-sharp – Nikolay Jul 21 '15 at 08:02

2 Answers2

2

As Serv wrote, you can do grouping before setting the datasource of your 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 then in your 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>
Community
  • 1
  • 1
Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
1

This should do it:

var list = new List<CustomItem>(){
    new CustomItem() { Department= "IT", UserName = "TestUser1"},
    new CustomItem() { Department = "Administration", UserName = "TestUser2"},
    new CustomItem() { Department = "Administration", UserName = "TestUser3"},
    new CustomItem() { Department = "Administration", UserName = "TestUser4"},
    new CustomItem() { Department = "Administration", UserName = "TestUser5"},
    new CustomItem() { Department = "Finance", UserName = "TestUser6"},
};

list.GroupBy (l => l.Department)
    .Select (l => new {
        Department = l.Key,
        Users = l.Select (x => x.UserName).Aggregate ((c,n) => c + ", " + n )
    });

Ouput:

enter image description here

They key thing is, that you first group by your Department item. Afterwards you select the username property and aggregate over them. What Aggregate() does is, it takes the current (c) and the next(n) item and then does what you specify with them. In this case it concatenates them with a comma and a whitespace ((c,n) => c + ", " + n). It then moves on to the next item

Demo Code

Marco
  • 22,856
  • 9
  • 75
  • 124
  • Thanks alot for the answer but I cannot Aggregate the `UserName`. In your example its a `string`. But in my case its a `Sitecore Item`. – Kamran Jul 21 '15 at 08:21
  • Can you parse it to a string? Or does this SiteCoreItem have any properties you can select from it on which you can aggregate? I'm not familiar with SiteCore in gerneral, but I'm pretty sure, you can somehow follow the above approach for your issue – Marco Jul 21 '15 at 08:22