Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按对象列表中的项分组(Sitecore项)并使用Repeater绑定_C#_Asp.net_Sitecore_Sitecore7 - Fatal编程技术网

C# 按对象列表中的项分组(Sitecore项)并使用Repeater绑定

C# 按对象列表中的项分组(Sitecore项)并使用Repeater绑定,c#,asp.net,sitecore,sitecore7,C#,Asp.net,Sitecore,Sitecore7,抱歉,我的描述太长,但我想让用户更容易理解我的问题。 我有一个包含部门项目和员工项目的列表 var empSubRelList = new List<EmpDeptRel>(); internal class EmpDeptRel { public Item DepartmentItem { get; set; } public Item EmployeeItem { get; set; } } 我用中继器绑定它: repEmployees.DataSource

抱歉,我的描述太长,但我想让用户更容易理解我的问题。 我有一个包含
部门项目
员工项目
的列表

 var empSubRelList = new List<EmpDeptRel>();

internal class EmpDeptRel
{
    public Item DepartmentItem { get; set; }
    public Item EmployeeItem { get; set; }
}
我用中继器绑定它:

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>
输出为:

IT TestUser1
Administration TestUser2
Administration TestUSer3
Administration TestUser4
Administration TestUser5
Finance TestUSer6
是否有可能将列表“empSubRelList”与部门分组,以便获得以下输出:

IT TestUser1
Administration TestUser2 TestUser3 TestUser4 TestUser5
Finance TestUser6
这应该做到:

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 )
    });
var list=新列表(){
新建CustomItem(){Department=“IT”,UserName=“TestUser1”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser2”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser3”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser4”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser5”},
新建CustomItem(){Department=“Finance”,UserName=“TestUser6”},
};
list.GroupBy(l=>l.Department)
.选择(l=>new{
部门=l.键,
Users=l.选择(x=>x.UserName).聚合((c,n)=>c+“,”+n)
});
输出:

他们的关键是,你们首先要按你们部门的项目分组。然后选择username属性并对其进行聚合。
Aggregate()
所做的是,它获取当前(
c
)和下一个(
n
)项,然后执行您指定的操作。在本例中,它用逗号和空格(
(c,n)=>c+“,“+n)
将它们连接起来。然后转到下一项

这应该可以做到:

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 )
    });
var list=新列表(){
新建CustomItem(){Department=“IT”,UserName=“TestUser1”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser2”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser3”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser4”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser5”},
新建CustomItem(){Department=“Finance”,UserName=“TestUser6”},
};
list.GroupBy(l=>l.Department)
.选择(l=>new{
部门=l.键,
Users=l.选择(x=>x.UserName).聚合((c,n)=>c+“,”+n)
});
输出:

他们的关键是,你们首先要按你们部门的项目分组。然后选择username属性并对其进行聚合。
Aggregate()
所做的是,它获取当前(
c
)和下一个(
n
)项,然后执行您指定的操作。在本例中,它用逗号和空格(
(c,n)=>c+“,“+n)
将它们连接起来。然后转到下一项

这应该可以做到:

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 )
    });
var list=新列表(){
新建CustomItem(){Department=“IT”,UserName=“TestUser1”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser2”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser3”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser4”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser5”},
新建CustomItem(){Department=“Finance”,UserName=“TestUser6”},
};
list.GroupBy(l=>l.Department)
.选择(l=>new{
部门=l.键,
Users=l.选择(x=>x.UserName).聚合((c,n)=>c+“,”+n)
});
输出:

他们的关键是,你们首先要按你们部门的项目分组。然后选择username属性并对其进行聚合。
Aggregate()
所做的是,它获取当前(
c
)和下一个(
n
)项,然后执行您指定的操作。在本例中,它用逗号和空格(
(c,n)=>c+“,“+n)
将它们连接起来。然后转到下一项

这应该可以做到:

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 )
    });
var list=新列表(){
新建CustomItem(){Department=“IT”,UserName=“TestUser1”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser2”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser3”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser4”},
新建CustomItem(){Department=“Administration”,UserName=“TestUser5”},
新建CustomItem(){Department=“Finance”,UserName=“TestUser6”},
};
list.GroupBy(l=>l.Department)
.选择(l=>new{
部门=l.键,
Users=l.选择(x=>x.UserName).聚合((c,n)=>c+“,”+n)
});
输出:

他们的关键是,你们首先要按你们部门的项目分组。然后选择username属性并对其进行聚合。
Aggregate()
所做的是,它获取当前(
c
)和下一个(
n
)项,然后执行您指定的操作。在本例中,它用逗号和空格(
(c,n)=>c+“,“+n)
将它们连接起来。然后转到下一项

如前所述,您可以在设置中继器的数据源之前进行分组:

IEnumerable dataSource=empSubRelList
.GroupBy(listItem=>Name(listItem.DepartmentItem))
.选择(分组=>new KeyValuePair(
分组键,
string.Join(“,”,grouping.Select(emp=>Name(emp.EmployeeItem)))
));
repEmployees.DataSource=数据源;
然后在html代码中:

<asp:Repeater runat="server" ID="repEmployees">
   <ItemTemplate>
       <li>
            <%# ((KeyValuePair<string, string>)Container.DataItem).Key %>
            <%# ((KeyValuePair<string, string>)Container.DataItem).Value %>
       </li>
   </ItemTemplate>
</asp:Repeater>

  • 如前所述,您可以在设置中继器的数据源之前进行分组:

    IEnumerable dataSource=empSubRelList
    .GroupBy(listItem=>Name(listItem.DepartmentItem))
    .选择(分组=>new KeyValuePair(
    分组键,
    string.Join(“,”,grouping.Select(emp=>Name(emp.EmployeeItem)))
    ));
    repEmployees.DataSource=数据源;
    
    然后在html代码中:

    <asp:Repeater runat="server" ID="repEmployees">
       <ItemTemplate>
           <li>
                <%# ((KeyValuePair<string, string>)Container.DataItem).Key %>
                <%# ((KeyValuePair<string, string>)Container.DataItem).Value %>
           </li>
       </ItemTemplate>
    </asp:Repeater>