所以下面是我目前擁有的代碼(這是行不通的,因爲最後一塊看到數組的定義,它是顯示我想要做的)C# - 如何創建一個KeyValuePair與兩個foreach循環的結果?
IEnumerable<string> sponsorshipQuery1 =
from runner1 in venueQuery6.Descendants("Runner")
where (int)runner1.Element("Sponsorship") > 0
orderby (int)runner1.Element("Sponsorship") descending
select runner1.Element("Firstname").Value;
IEnumerable<string> sponsorshipQuery2 =
from runner1 in venueQuery6.Descendants("Runner")
where (int)runner1.Element("Sponsorship") > 0
orderby (int)runner1.Element("Sponsorship") descending
select runner1.Element("Sponsorship").Value;
string[] nameArray;
string[] moneyArray;
foreach (string name in sponsorshipQuery1)
{
nameArray = sponsorshipQuery1
.OfType<object>()
.Select(o => o.ToString()).ToArray();
}
foreach (string money in sponsorshipQuery2)
{
moneyArray = sponsorshipQuery2
.OfType<object>()
.Select(o => o.ToString())
.ToArray();
}
Console.WriteLine("All sponsorship money raised in descending order:");
for (int i = 0; i < nameArray.Length; i++)
{
Console.WriteLine(nameArray[i] + " has raised £" + moneyArray[i]);
}
理想情況下,我希望能夠在底部執行for循環。我想在替換兩個foreach循環,並使他們KeyValuePairs但我不知道該怎麼做。
我也是新來使用LINQ查詢,所以我之所以有查詢語法的混合物=>語法是因爲我被教導的查詢語法,但我也想代碼中,我就發現互聯網。
我不知道,你可以使用'用括號選擇new',從我瞭解到我只選擇單個值。這工作,謝謝! –
樂意幫忙.. – stuartd
@TerminalGuy它*是一個單值,但是這個單值可以包含多個:) –