GroupBy特有的行为

时间:2014-12-30 05:52:37

标签: c# linq

为什么Group会返回所有元素(事实上,在我的情况下,所有元素都超过8个元素![在此输入图像描述] [1])

 List<Package> packages = new List<Package> { new Package { Company = "Coho Vineyard", Weight = 25.2, TrackingNumber = 10 },
                                             new Package { Company = "Lucerne Publishing", Weight = 18.7, TrackingNumber = 10},
                                             new Package { Company = "Wingtip Toys", Weight = 6.0, TrackingNumber = 12 },
                                             new Package { Company = "Contoso Pharmaceuticals", Weight = 9.3, TrackingNumber = 13},
                                             new Package { Company = "Wide World Importers", Weight = 33.8, TrackingNumber = 12 } };

IGrouping<long, Package>[] simba_3 = packages.GroupBy(x => 23L).ToArray();

simba_3有一个元素,其中key = 23和8个Package元素,这里发生了什么?

1 个答案:

答案 0 :(得分:0)

因为您没有以正常方式使用GroupBy

List<Package> packages = new List<Package> { new Package { Company = "Coho Vineyard", Weight = 25.2, TrackingNumber = 10 },
                                             new Package { Company = "Lucerne Publishing", Weight = 18.7, TrackingNumber = 10},
                                             new Package { Company = "Wingtip Toys", Weight = 6.0, TrackingNumber = 12 },
                                             new Package { Company = "Contoso Pharmaceuticals", Weight = 9.3, TrackingNumber = 13},
                                             new Package { Company = "Wide World Importers", Weight = 33.8, TrackingNumber = 12 } };

var simba_3 = packages.GroupBy(x => x. Weight).ToArray();
var simba_4 = packages.GroupBy(x => x. TrackingNumber).ToArray();