-7

I have got a List<> with 5k words in it.

var abc = new List<ClassName>();

I want to retrieve the frequent/highest repeated word from List<> and return it. Thoughts?

 public class Datum
    {
        public string name { get; set; }
        public string id { get; set; }
        public string pic_large { get; set; }
    }
Coding Freak
  • 408
  • 1
  • 6
  • 26

2 Answers2

1

This will get the most popular instance of Datum in your abc List

var mostPopular = abc
  .GroupBy(x => x.name)
  .OrderByDescending(g => g.Count())
  .First();

If you want JUST the name value of the most popular append a Select on the end

var mostPopularName = abc
  .GroupBy(x => x.name)
  .OrderByDescending(g => g.Count())
  .First()
  .Select(x=> x.name);
Alex
  • 34,123
  • 47
  • 189
  • 315
1
var result = abc
    .GroupBy(x => x.name)
    .OrderByDescending(grouped => grouped.Count())
    .First();

That LINQ query is taking your list, grouping it by name property, so you are getting objects: name, Collection<ClassName>.

You want to Order them descending by it's Count and take First

madoxdev
  • 3,072
  • 1
  • 16
  • 36