Home > OS >  Order by duplicate amount in a list using linq
Order by duplicate amount in a list using linq

Time:03-23

I have this class:

class Cell
{
    string keyword;
    double bkgColor;
    double keywordColor;
}

I have a list of this class and I want to order it by duplicates of bkgColor using linq. For example: Keywords with background color that has the most keywords assigned, would be at the start of the list etc.

Evey answer is highly appreciated!

CodePudding user response:

Assuming that keyword, bkgColor and keywordColor are public properties of the class Cell. You can use this linq

var result = cells.GroupBy(c => c.bkgColor)
            .OrderByDescending(g => g.Count())
            .SelectMany(g => g.Select(c => c));
  • Related