2014-10-17 38 views
0

選擇很多,我想在陣列中的一些位置來選擇所有「當事人」:LINQ的從考慮下面的代碼嵌套列表

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); 

我想是讓所有締約方的名單所有Filings ...例子的第0個位置將返回p1,p4,p7和p10。我想:

fList.SelectMany(f => f.Parties[0]); 

...但得到一個編譯錯誤,指出SelectMany不能從使用推斷。有任何想法嗎?

回答

0

SelectMany假定它的參數將返回一個IEnumerable。你的論點返回一個單一的對象;因此一個簡單的Select比SelectMany更合適。

0

ALT1

變種yourValues = fList.SelectMany(X => x.Parties.Take(1))選擇(S => s.Name)。

ALT2

變種yourValues = fList.Select(X => x.Parties.First()的名稱。);

foreach (var val in yourValues) 
{ 
    Console.WriteLine(val); //p1 p4 p7 p10 
}