C# - 如何使用两个foreach循环的结果创建KeyValuePair?

时间:2023-01-27 22:58:03

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.

理想情况下,我希望能够在底部执行for循环。我猜想要更换两个foreach循环并使它们成为KeyValuePairs,但我不知道该怎么做。

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.

我也是使用Linq查询的新手,所以我混合使用查询语法和=>语法是因为我学习了查询语法,但我也在尝试在互联网上找到的代码。

2 个解决方案

#1


6  

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

#2


2  

The highest-level way of iterating two sequences in tandem is with Zip. To get KeyValuePairs I'd use

串联迭代两个序列的*方法是使用Zip。要获得KeyValuePairs,我会使用

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.

虽然我可能根本不使用KeyValuePair,但更具体的是我的任务或匿名类型,除非我真正关心它作为键和值对。

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

迭代你已经涵盖的两个数组。要对两个不能(必然)编入索引的IEnumerable 执行双重操作,需要将至少一个循环分解为其组件MoveNext()和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.

我只是选择新的KeyValuePair (runner1.Element(“Firstname”)。值,runner.Element(“赞助”)。值)。 ,string>

#1


6  

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

#2


2  

The highest-level way of iterating two sequences in tandem is with Zip. To get KeyValuePairs I'd use

串联迭代两个序列的*方法是使用Zip。要获得KeyValuePairs,我会使用

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.

虽然我可能根本不使用KeyValuePair,但更具体的是我的任务或匿名类型,除非我真正关心它作为键和值对。

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

迭代你已经涵盖的两个数组。要对两个不能(必然)编入索引的IEnumerable 执行双重操作,需要将至少一个循环分解为其组件MoveNext()和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.

我只是选择新的KeyValuePair (runner1.Element(“Firstname”)。值,runner.Element(“赞助”)。值)。 ,string>