我有一个班级:
public class Member
{ public string header {get;set;}
public List<Member> MemberCollection {get;set;}
}
所以我有List 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
答案 0 :(得分:4)
这样的事情:
List<Member> firstLevels = new List<Member>();
foreach(Member m in lstMembers)
{
firstLevels.Add(m);
firstLevels.Add(m.MemberCollection.First());
}
答案 1 :(得分:0)
好吧,Member A
&amp; Member B
不是他们的第一级
但是如果要获得Member A.1, Member B.1 ...
members.Select(MemberList => MemberList.MemberCollection.FirstOrDefault()).ToList();
答案 2 :(得分:0)
如果允许使用LINQ,则可以在集合上构建表达式。使用带有order by子句的SelectMany运算符。
注意:以下代码只是为了演示这个想法并忽略任何语法/案例问题
var members = new List<Member>();
var q = members.SelectMany(m => m.MemberCollection).Orderby(o=>o.Name).FirstOrDefault()
.ToList();