user215675
user215675

Reputation: 5181

C# Grouping numbers another way

When grouping numbers ,I use

   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 };

When i suppose to use

var query = numbers.GroupBy() ( I do't know how to name it ,is it extension chaining ?)

what is the way to do it?

Upvotes: 0

Views: 181

Answers (2)

Rubens Farias
Rubens Farias

Reputation: 57946

Using same semantics, you'll have:

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

Upvotes: 4

Blake Pettersson
Blake Pettersson

Reputation: 9067

I'm assuming that you want to know how to use the extension methods to do the same thing. In this case it would be

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

Upvotes: 6

Related Questions