次のリストをlinqで最も多く出現する降順でソートする方法を考えています:
ghj
def
abc
def
abc
abc
に:
abc
def
ghj
ラムダ式を探しています。
string[] names = { "ghj", "def", "abc", "def", "abc", "abc" };
IEnumerable<string> query = names
.GroupBy(s=>s) // groups identical strings into an IGrouping
.OrderByDescending( group => group.Count()) // IGrouping is a collection, so you can count it
.Select(group=>group.Key); // IGrouping has a Key, which is the thing you used to group with. In this case group.Key==group.First()==group.skip(1).First() ...
出現順に並べられた個別のリストを取得する場合は、Group By を使用します。
var query = foo.GroupBy(xx => xx)
.OrderByDescending(gg => gg.Count())
.Select(gg => gg.Key);
// on your input returns:
// abc
// def
// ghj