【发布时间】:2015-11-30 22:27:16
【问题描述】:
我有三个班级:
BargeGrouper 类实现了IBargeGrouper 并具有Group(IEnumerable<Barge> ungrouped) 方法。
BargeGroupMap 类实现了IBargeGroupShow 并具有Show(IEnumerable<BargeGroup> 方法。
如果这两个都是我调用的类:GroupingModule,它是Run()。
问题是,当我调用Group(ungrouped); 然后想Show(bargeGroups); 我在Show(bargeGroups); 中得到IndexOutOfRangeexception,因为IEnumerable 我作为参数传递给它的Count 属性比实际多得多其中的元素。我发现尽管我使用ContinueWith 用BargeGroup 元素填充该集合,但大多数时候分组集合从Group 返回为空。
BargeGrouper的Group方法
public IEnumerable<BargeGroup> Group(IEnumerable<Barge> ungroupedBarges)
{
List<BargeGroup> bargeGroups = new List<BargeGroup>();
Int32 groupNumber = 0;
var riverBarges = from barge in ungroupedBarges
where barge != null && !String.IsNullOrEmpty(String.Intern(barge.River))
let river = String.Intern(barge.River)
orderby river, barge.MileMarker ascending
group barge by river into barges
select barges.AsEnumerable();
foreach (IEnumerable<Barge> barges in riverBarges)
{
Task.Run(() => ResolveRiver(barges, ref groupNumber)).ContinueWith(t=>
{
IEnumerable<BargeGroup> riverGroups = t.Result;
bargeGroups.AddRange(riverGroups);
});
}
return bargeGroups;
}
ShowBargeGroups:
public void ShowBargeGroups(IEnumerable<BargeGroup> bargeGroups)
{
Console.WriteLine("Barge groups :");
if (bargeGroups != null)
{
foreach (var group in bargeGroups.Where(b => b != null))
{
var title = String.Format("{0}\n\t | {1} \t\t | {2} \t | {3} \t|", group.Id, "Id", "River", "MileMarker");
Console.WriteLine(title);
foreach (var barge in group.Barges)
{
var caption = String.Format("\t | {0}\t | {1} \t | {2} \t|", barge.Id, barge.River, barge.MileMarker);
Console.WriteLine(caption);
}
}
}
}
以及GroupingModule:.中的用法。
var groupBarges = bargeGrouper.Group(barges);
bargeGroupShow.ShowBargeGroups(groupBarges);
我能做些什么来解决这个问题?
【问题讨论】:
-
告诉我们
ShowBargeGroups。您使用的是哪个 .NET 版本? -
@YuvalItzchakov 请看更新。我使用.net 4.5。谢谢。
标签: c# async-await task-parallel-library task