2014-12-30 42 views
0

爲什麼通過回做集團的所有元素(其實都不止,8種元素在我的情況![在這裏輸入的形象描述] [1])的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 } }; 

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

simba_3有鍵一個元素= 23和8封裝元素,這裏發生了什麼?

+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

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(); 
+0

不,請參閱PetSerA的完美答案,這就是爲什麼它返回所有(所謂的)不存在的鍵的所有元素 –