简体   繁体   中英

group List<T> and create new list with count

I have next class and list:

public class cls1
{
  public string prop1 {get; set;}
  public string prop2 {get; set;}
  public string prop3 {get; set;}
}

public List<cls1> list = new List(cls1)();

I need to group list by prop2 field and count this field and on the end order it descending by count.

You can do:

var query = list.GroupBy(r => r.prop2)
                .Select(grp => new
                {
                    Key = grp.Key,
                    Count = grp.Count(),
                })
                .OrderByDescending(r => r.Count);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM