-4

我有一堂课:

public class Member 
{    public string header {get;set;}    
     public List<Member> MemberCollection {get;set;} 
}

所以我有列表 lstMembers;包含这样的:

MemberA
   |_____ header = HeadingMemberA
   |
   |_____ List<Member> = Member A.1
                    |___ Member A.2
                    |___ Member A.3

MemberB
   |_____ header = HeadingMemberB
   |
   |_____ List<Member> = Member B.1
                    |___ Member B.2
                    |___ Member B.3

所以现在我必须在每个级别获得第一个成员。

列出成员 A、成员 A.1、成员 B 和成员 B.1

4

3 回答 3

4

像这样的东西:

List<Member> firstLevels = new List<Member>();
foreach(Member m in lstMembers)
{
   firstLevels.Add(m);
   firstLevels.Add(m.MemberCollection.First());
}
于 2013-05-30T07:55:30.163 回答
0

如果您被允许使用 LINQ,那么您可以在您的集合上构建一个表达式。将 SelectMany 运算符与 order by 子句一起使用。

注意:以下代码只是为了演示这个想法并忽略任何语法/案例问题

var members = new List<Member>();
var q = members.SelectMany(m => m.MemberCollection).Orderby(o=>o.Name).FirstOrDefault()
 .ToList();
于 2013-05-30T07:57:49.143 回答
0

好吧,Member AMember B他们的水平上不是第一

但如果想要得到Member A.1, Member B.1 ...

members.Select(MemberList => MemberList.MemberCollection.FirstOrDefault()).ToList();
于 2013-05-30T07:57:27.143 回答