Reputation: 188
So below is the code that I currently have (it doesn't work because the last block sees the arrays an undefined, it's to show what I want to do)
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]);
}
Ideally, I want to be able to execute the for loop at the bottom. I'd guess at replacing the two foreach loops and making them KeyValuePairs but I don't know how to do that.
I'm also new to using Linq queries so the reason why I have a mixture of query syntax and => syntax is because I was taught the query syntax, but I'm also trying code I've found on the internet.
Upvotes: 0
Views: 376
Reputation: 113382
The highest-level way of iterating two sequences in tandem is with Zip
. To get KeyValuePair
s I'd use
sponsorshipQuery1.Zip(sponsorshipQuery2, (k, v) => new KeyValuePair<string, string>(k, v))
Though I'd likely not use KeyValuePair
at all, but something more specific to my task or an anonymous type, unless I truly cared about it as a key and value pair.
Iterating two arrays you've already covered. To do a double-foreach
on two IEnumerable<T>
that can't (necessarily) be indexed one needs to break down at least one of the loops to its component MoveNext()
and Current
using (var en1 = sponsorshipQuery1.GetEnumerator())
{
foreach(var item2 in sponsorshipQuery2)
{
if (!en1.MoveNext())
{
break;
}
var item1 = en.Current;
// Do something with the two items here. E.g.
}
}
In your particular example though since you have a common source of:
from runner1 in venueQuery6.Descendants("Runner")
where (int)runner1.Element("Sponsorship") > 0
orderby (int)runner1.Element("Sponsorship") descending
I'd just select new KeyValuePair<string, string>(runner1.Element("Firstname").Value, runner.Element("Sponsorship").Value)
to begin with.
Upvotes: 2
Reputation: 73313
It appears from your example code that you can just get both values in one query, and keep them in an anonymous type:
var results = from runner1 in venueQuery6.Descendants("Runner")
where (int)runner1.Element("Sponsorship") > 0
orderby (int)runner1.Element("Sponsorship") descending
select new {
Name = runner1.Element("Firstname").Value,
Amount = runner1.Element("Sponsorship").Value
};
And then you can loop over these results:
foreach (var result in results)
{
Console.WriteLine(result.Name + " has raised £" + result.Amount);
}
Upvotes: 6