user10208134
user10208134

Reputation:

How to get repeated values in LINQ

var numbs4 = new List<dynamic>() { 12, 9, 75,75, 23, 12, 33, 34, 54, 21, 22 };

Hi how do I get {75,75} from an ascending order using LINQ?

Using Take() you limit the number of rows

var result = numbs4.OrderByDescending(x=>x).Skip(1).Take(1).ToList();

Result : 75

I want the identical to be involved as well: {75,75} Take restrict you, what I am going to do?

Upvotes: 1

Views: 44

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726869

If you would like to get all items matching the first-after-max, you could group by, order groups by the key in descending order, and pick the entire initial group, like this:

var result = numbs4
    .GroupBy(x =>x)
    .OrderByDescending(g => g.Key)
    .Skip(1)
    .First()            // That's the second group now
    .ToList();

Demo.

Upvotes: 2

Related Questions