title: LINQ IQueryable with group by and count
body: <p>In my .net Core 2.0 Razor Pages project, I am declaring a List like this, where MyData is a model linked to an Entity Framework dataset:</p>
  
  <pre><code>public IList&lt;MyData&gt; MyData { get; set; }
  </code></pre>
  
  <p>For simplicity, let's say my data looks like this:</p>
  
  <pre><code>ID | UserName | LogOn
  1  | User1    | 25/03/2018 12:54
  2  | User2    | 25/03/2018 09:43
  3  | User1    | 24/03/2018 18:23
  4  | User3    | 24/03/2018 08:16
  5  | User2    | 23/03/2018 17:12
  </code></pre>
  
  <p>..etc</p>
  
  <p>Then in my OnGet() method I know I can query this to produce a list I can loop through, like so:</p>
  
  <pre><code>IQueryable&lt;MyData&gt; MyDataIQ = from i in _context.MyData select i;
  
  MyData = await MyDataIQ.ToListAsync();
  </code></pre>
  
  <p>If I wanted to return a list which is grouped by UserName with a count of all occurrences of that UserName, what would my LINQ query look like?</p>
  
  <p>I've tried this:</p>
  
  <pre><code>IQueryable&lt;MyData&gt; MyDataIQ = from i in _context.MyData group i by i.UserName into grp select new {key = grp.Key, cnt = grp.Count()};
  </code></pre>
  
  <p>But this just gives me a type conversion error.</p>
  
  <p>I have the raw data, I just want to show it on the view grouped in this fashion.</p>
  
  <p>I'm new to Core 2.0 and also Linq, so any help will be massively useful.</p>