[英]Linq Select Many from nested list
給定以下代碼,我想在數組中的某個位置選擇所有“參與者”:
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
。 有任何想法嗎?
SelectMany假定其參數將返回IEnumerable。 您的參數返回單個對象; 因此,簡單的Select比SelectMany更合適。
Alt1
var yourValues = fList.SelectMany(x => x.Parties.Take(1))。Select(s => s.Name);
Alt2
var yourValues = fList.Select(x => x.Parties.First()。Name);
foreach (var val in yourValues)
{
Console.WriteLine(val); //p1 p4 p7 p10
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.