我有以下代码对匿名集合中的值进行分组和排序。当我处于排序循环中时,我看到值已正确排序。循环后,它们仍然在未排序的地方..... 为什么?class Test{ public Guid P1; public int P2;}var Lista = new List<Test>();Lista.Add(new Test() { P1 = Guid.Parse("D4651E83-67D6-4BCD-B28D-8173C5AE94F7"), P2 = 2 });Lista.Add(new Test() { P1 = Guid.Parse("D4651E83-67D6-4BCD-B28D-8173C5AE94F7"), P2 = 1 });Lista.Add(new Test() { P1 = Guid.Parse("310D74C4-0624-4B1C-9F4E-CB85924CB2B6"), P2 = 4 });Lista.Add(new Test() { P1 = Guid.Parse("310D74C4-0624-4B1C-9F4E-CB85924CB2B6"), P2 = 3 });var groups = from g in Lista group g.P2 by g.P1 into g select new { KeyGroup = g.Key, Rect = g.ToList() };foreach (var x in groups){ x.Rect.Sort((a, b) => (a.CompareTo(b))); }
1 回答
炎炎设计
TA贡献1808条经验 获得超4个赞
为什么?
因为groups是一个可枚举的序列而不是一个集合。在迭代之前调用ToList()或调用ToArray()它:
var groups = (from g in Lista
group g.P2 by g.P1 into g
select new { KeyGroup = g.Key, Rect = g.ToList() }).ToArray();
- 1 回答
- 0 关注
- 150 浏览
添加回答
举报
0/150
提交
取消