2

Please look at the following code snippet, and the question is presented in the inner foreach loop, which is to ask will the final grouped result keep the original orderby result?

//List<Foo> aFooList = ...
aFooList = aFooList.OrderBy(x=>x.A).toList(); //order the list by property A
var groupedResult = aFooList.GroupBy(x=>x.B)  //group the list by property B
                            .OrderBy(x=>x.Count(y=>y!=null)) //Order the group by the group size.
                            .ToList();
foreach(var group in groupedResult)
{
    Debug.WriteLine(group.key);
    foreach(var foo in group)
    {
         //QUESTION: will the foo list here keep the original order which is ordered by property A???
    }
}
dingx
  • 1,621
  • 3
  • 20
  • 38
  • 1
    Why not just try it with some test data? :-) – DerApe Oct 17 '14 at 08:04
  • 2
    @derape: Because it coming out in the same order once is not necessarily indicative of it doing the same every time. Much better to have somebody that can state authoritatively with reference to spec or code. A classic example of this is the ordering of SQL queries without a specific order by where it depends on how they are stored as to what order they come out in. I will agree that in this case though working once probably will mean working every time. – Chris Oct 17 '14 at 08:06
  • 1
    thanks for your comments, but test data can only convince me if the answer is No, but it has little to say when the answer is Yes. – dingx Oct 17 '14 at 08:08
  • 4
    From [MSDN](http://msdn.microsoft.com/en-us/library/vstudio/bb534304(v=vs.100).aspx): _"The IGrouping objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping. Elements in a grouping are yielded in the order that the elements that produced them appear in source."_ – Tim Schmelter Oct 17 '14 at 08:09

0 Answers0