TheDude
TheDude

Reputation: 197

Selecting objects from a grouped linq query

I have a collection of objects that I group with a L2O linq query.

The results of that query are as follows:

L2O query objects

Now, I want to transform this query into a query that returns the highest number of each key.

The only thing I got until now is the following:

var orderStuff = from i in collection
                 group i by i.Letter;

But I am having a real hard time to expand on this.

Upvotes: 1

Views: 147

Answers (1)

SLaks
SLaks

Reputation: 888177

Your query returns a set of IGrouping<String, int>. This type has a Key property with the group's label, and inherits IEnumerable<int> containing the items in the group.

You want to select the Max() of each group.

For example:

var orderStuff = from i in collection
                 group i by i.Letter into g
                 select new { Letter = g.Key, Max = g.Max() };

Upvotes: 1

Related Questions