3

次のリストをlinqで最も多く出現する降順でソートする方法を考えています:

ghj
def
abc
def
abc
abc

に:

abc
def
ghj

ラムダ式を探しています。

4

2 に答える 2

5
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() ...
于 2012-04-23T15:29:37.970 に答える
2

出現順に並べられた個別のリストを取得する場合は、Group By を使用します。

var query = foo.GroupBy(xx => xx)
               .OrderByDescending(gg => gg.Count())
               .Select(gg => gg.Key);
// on your input returns:
// abc
// def
// ghj
于 2012-04-23T15:30:05.340 に答える