[英]LINQ: Order By Count of most common value
我正在尝试在c#中创建一个“有序”对象列表,其中列表将按ImdbId属性中最常见的值排序。 我没有使用数据库我从列表中的远程api获得结果,我只需要以优雅的方式对列表进行排序。
class Movie
{
String ImdbId {get; set; }
String Name {get;set;}
... more properties
}
未排序列表的示例:
imdbId | Name
698649 | "Sex and the City" Models and Mortals
698669 | "Sex and the City" The Awful Truth
698649 | "Sex and the City" Models and Mortals
698649 | "Sex and the City" Models and Mortals
698679 | "Sex and the City" The Drought
698697 | "Sex and the City" Valley of the Twenty-Something Guys
1568911 | War Horse
排序应该如下所示:
imdbId | Name
698649 | "Sex and the City" Models and Mortals
698649 | "Sex and the City" Models and Mortals
698649 | "Sex and the City" Models and Mortals
698669 | "Sex and the City" The Awful Truth
698679 | "Sex and the City" The Drought
698697 | "Sex and the City" Valley of the Twenty-Something Guys
1568911 | War Horse
这是另一个例子:
imdbId | Name
1568911 | War Horse
698690 | "Sex and the City" The Turtle and the Hare
698653 | "Sex and the City" Old Dogs, New Dicks
698690 | "Sex and the City" The Turtle and the Hare
698690 | "Sex and the City" The Turtle and the aHare
1000774 | Sex and the City
排序应该如下所示:
imdbId | Name
698690 | "Sex and the City" The Turtle and the Hare
698690 | "Sex and the City" The Turtle and the Hare
698690 | "Sex and the City" The Turtle and the aHare
698653 | "Sex and the City" Old Dogs, New Dicks
1000774 | Sex and the City
1568911 | War Horse
我试着追随但没有运气。
List<Movie> newList = List
.OrderByDescending(a => a.ImdbId)
.ThenByDescending(a => a.ImdbId.Count())
.ToList();
知道如何用lambda或LINQ实现这个目标吗?
尝试这个:
var newList = List.GroupBy(x=>x.ImdbId)
.OrderByDescending(g=>g.Count())
.SelectMany(g=>g).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.