首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按重复次数排序LINQ ASP.NET MVC

按重复次数排序LINQ ASP.NET MVC
EN

Stack Overflow用户
提问于 2022-05-01 15:33:22
回答 1查看 85关注 0票数 0

我有一个Report模型:

代码语言:javascript
复制
public class Report
{
    [Key]
    public int Id { get; set; }
    public string ReporterId { get; set; }
    [ForeignKey("ReporterId")]
    public virtual ApplicationUser Reporter { get; set; }
    public int ProductId { get; set; }
    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
    public ReportType ReportType { get; set; }
}

现在,我希望将基于List<Report>的最重复元素排序的ProductId返回到重复次数最少的元素。

有没有办法用linq来做这件事?

EN

回答 1

Stack Overflow用户

发布于 2022-05-01 16:09:14

试试这个:

代码语言:javascript
复制
var newList = List.GroupBy(p=> p.ProductId )
              .OrderByDescending(x=> x.Count())
              .SelectMany(x=> x).ToList();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72078133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档