鉴于以下代码,我想在阵列中的某个位置选择所有“派对”:
var f1 = new Filing();
var f2 = new Filing();
var f3 = new Filing();
var f4 = new Filing();
var p1 = new Party() {Name = "p1"};
var p2 = new Party() { Name = "p2" };
var p3 = new Party() { Name = "p3" };
var p4 = new Party() { Name = "p4" };
var p5 = new Party() { Name = "p5" };
var p6 = new Party() { Name = "p6" };
var p7 = new Party() { Name = "p7" };
var p8 = new Party() { Name = "p8" };
var p9 = new Party() { Name = "p9" };
var p10 = new Party() { Name = "p10" };
var p11 = new Party() { Name = "p11" };
var p12 = new Party() { Name = "p12" };
var p1List = new List<Party>();
p1List.Add(p1);
p1List.Add(p2);
p1List.Add(p3);
f1.Parties = p1List;
var p2List = new List<Party>();
p2List.Add(p4);
p2List.Add(p5);
p2List.Add(p6);
f2.Parties = p2List;
var p3List = new List<Party>();
p3List.Add(p7);
p3List.Add(p8);
p3List.Add(p9);
f3.Parties = p3List;
var p4List = new List<Party>();
p4List.Add(p10);
p4List.Add(p11);
p4List.Add(p12);
f4.Parties = p4List;
var fList = new List<Filing>();
fList.Add(f1);
fList.Add(f2);
fList.Add(f3);
fList.Add(f4);
我想要的是获得所有会员在第0位的所有会员的列表...示例将返回p1,p4,p7和p10。我试过了:
fList.SelectMany(f => f.Parties[0]);
...但是收到编译错误,指出无法从使用中推断出SelectMany
。有什么想法吗?
答案 0 :(得分:0)
SelectMany假定其参数将返回IEnumerable。你的参数返回一个对象;因此,简单的Select比SelectMany更合适。
答案 1 :(得分:0)
Alt1
var yourValues = fList.SelectMany(x =&gt; x.Parties.Take(1))。选择(s =&gt; s.Name);
ALT2
var yourValues = fList.Select(x =&gt; x.Parties.First()。Name);
foreach (var val in yourValues)
{
Console.WriteLine(val); //p1 p4 p7 p10
}