2009-12-02 57 views
0

在分組號,我用C#分組數的另一種方式

string[] numbers = 
    { "123", "34555", "91882", "100", "7823", "1111", "76551" }; 

    var query = from digits in numbers 
       group digits by digits.Length into ByDigit 
       select 
       new { digit = ByDigit, length = ByDigit.Key }; 

當我想用

var query = numbers.GroupBy() (我do't知道如何命名,是擴展鏈接?)

這是幹什麼的?

回答

6

我假設你想知道如何使用擴展方法來做同樣的事情。在這種情況下,將

var query = numbers.GroupBy(n => n.Length).Select(n => new { digit = n, length = n.Key }); 
+0

哦!是的,我的意思是擴展方法 – user215675 2009-12-02 14:19:04

4

使用相同的語義,你必須:

var query = numbers 
    .GroupBy(digits => digits.Length) 
    .Select(ByDigit => new 
    { 
     digit = ByDigit, 
     length = ByDigit.Key 
    }); 
+0

你太快了:) – GraemeF 2009-12-02 14:21:48