-1

I have a list of strings which I group by their occurrence in the list like following (where key is the list of those strings):

  mostCommonKeywords = key.GroupBy(v => v)
                            .OrderByDescending(g => g.Count()) // here I get the count number
                            .Select(g => g.Key)
                            .Distinct()
                            .ToList();

The thing I want to do now is when they are sorted out, I wanna get their count, since LINQ can clearly distinguish their number and sort them out... How can I get the count occurrence of each string in the list now ???

Edit:

Let's say I have count values that look like this:

124
68
55
48
32
19
13
10

I cannot simply Add 1 of this value into a variable called "Count" as @octavioccl suggested. I clearly have to store them into some kind of list or something...

User987
  • 3,663
  • 15
  • 54
  • 115
  • You don't need the `Distinct` since the result of `GroupBy` will be a set of unique keys. Also to have the count just include it in the `Select`. – juharr Oct 24 '16 at 18:52

2 Answers2

8

Using the Select and projecting in an anonymous type:

 var result=key.GroupBy(v => v)
               .Select(g => new {g.Key, Count=g.Count()})
               .OrderByDescending(e => e.Count) 
            // .Distinct()// Don't need this call
               .ToList();

Update

You can also project your query using a DTO:

public class CustomDTO
{
  public string Key{get;set;} // Change the type in case you need to
  public int Count{get;set;}
}

So, your query would be:

 var result=key.GroupBy(v => v)
               .Select(g => new CustomDTO{Key=g.Key, Count=g.Count()})
               .OrderByDescending(e => e.Count) 
               .ToList();
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • Imagine I have 500 of these... I'm gonna need more than just a simple variable to store 1 value.. We are talking about 500 different values, I'll need to store them into a list or something all at once... Do you understand what I'm trying to say? :) – User987 Oct 24 '16 at 18:49
  • Not really :(, could you update your question with an example – ocuenca Oct 24 '16 at 18:51
  • @User987 your edit is exactly what this answer does. Check the .Key variable – Camilo Terevinto Oct 24 '16 at 18:54
  • Oh goodness it seems ur right... This is it... By the way , what type will the result be?? It's clearly not just a generic list? – User987 Oct 24 '16 at 18:58
  • Excellent excellent, this is even better than I've imagined. Thanks mate!! :) Accepted ur answer ! :D – User987 Oct 24 '16 at 19:00
  • @User987 It's an [anonymous type](https://msdn.microsoft.com/en-us/library/bb397696.aspx). – juharr Oct 24 '16 at 19:00
  • Although if you're selecting a CustomDTO it could be a CustomDTO type. – JonH Oct 24 '16 at 19:02
-1
from a in keys
group a by a.key into g
select new { a.Key, Count = g.Count() };
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Ghost Developer
  • 1,283
  • 1
  • 10
  • 18
  • 2
    You mean `group a by a into g` Also why delete an answer and just create another one that's basically the same? – juharr Oct 24 '16 at 18:53
  • 1
    The important thing here isn't to use query syntax instead of method syntax, but the fact that you include the `Count` in the select. But currently your group by is wrong because `a` is a `string` and does not have a `key` member. – juharr Oct 24 '16 at 18:57