我有一个这样的循环:
this.results = new List<Tuple<int, IEnumerable<Thing>>>();
var utcNow = DateTime.UtcNow;
var resultsLocker = new object();
Parallel.ForEach(
this.dataHelper.GetActiveIds(),
id =>
{
var result = new Tuple<int, IEnumerable<Thing>>(
id,
this.dataHelper.GetThing(id, this.PossibleLastRunTime, utcNow));
lock (resultsLocker)
{
this.results.Add(result);
}
});
并且,使用这个答案,翻译成更紧凑和易于理解的:
this.results = this.dataHelper.GetActiveIds()
.AsParallel()
.Select(id => new Tuple<int, IEnumerable<Thing>>(
id,
this.dataHelper.GetThing(id, this.PossibleLastRunTime, utcNow)))
.ToList();
现在我有一个更复杂的嵌套循环:
var measuresLocker = new object();
var measures = new List<Tuple<int, object, object>>();
Parallel.ForEach(
this.results,
result =>
{
foreach (var measuredValue in result.Item2.Select(destination =>
new Tuple<int, object, object>(
result.Item1,
destination.Message,
destination.DestinationName)))
{
lock (measuresLocker)
{
measures.Add(measuredValue);
}
}
});
我想做类似的事情,但我被这段代码卡住了:
measures = this.results
.AsParallel()
.Select(result => result.Item2.Select(destination =>
new Tuple<int, object, object>(
result.Item1,
destination.Message,
destination.DestinationName)).ToList()).ToList();
我似乎得到了一个列表列表,我只想要一个按照我的原始代码的列表。这可以使用 LINQ 非常简洁地完成吗?如果是这样,怎么办?