Skip to content

Codepolice

  • ⤫

Join and Group By with LINQ to Entities

Posted by Judy Alvarez Posted on February 25, 2022March 3, 2022
0

I have banged my head against some LINQ to Entities stuff today. It started out when I was doing a query to get some stats out of my forum. I should point out that I’m a newbe with LINQ to Entities and the Entity Framework so if I have done anything stupid here you are free to mock me in the comments.

var query = (from s in db.ForumStatsSet
where s.LogDate >= date1 && s.LogDate <= date2
group s by new {
  s.Topic.topicID, s.Topic.subject,
  s.Topic.datum, s.Topic.Forum.forumID,
  s.Topic.Forum.forumName,
  s.Topic.Forum.ForumGroup.name } into g
orderby g.Count() descending
select new TopicStatsData
  {
  TopicId = g.Key.topicID,
  Count = g.Count(),
  Subject = g.Key.subject,
  ForumId = g.Key.forumID,
  ForumName = g.Key.forumName,
  ForumGroupName = g.Key.name
});

This code looked fine to me. It’s kind of massive and has a lot of joins but it should be used in an admin interface so i did not care that much. But it turned out that it timeout every time I ran it. I had a look at the SQL that was generated and that was not a pretty sight. A massive amount of subqueries and left outer joins.

I posted a question on the new and really really good code community Stackoverflow and got the tip that I should join the tables before group by clue. And that is what I tried to do.

The problem was that I got this error all the time.

from s in DB.ForumStatsSet
from t in s.Topic

Error:
An expression of type ‘WhoaAdmin.Models.Topic’ is not allowed in a subsequent from a clause in a query expression with source type ‘System.Data.Objects.ObjectQuery<WhoaAdmin.Models.ForumStats>’. Type inference failed in the call to ‘SelectMany’.

I tried to google that error and I got nothing. I realized that maybe you have to do the joins in the correct order and that was it and I ended up with this query.

var query = (from fg in db.ForumGroupSet
from f in fg.Forums
from t in f.Topics
from s in t.ForumStats
where s.LogDate >= date1 && s.LogDate <= date2
group s by new { t.topicID,
  t.subject,
  t.datum,
  f.forumID,
  f.forumName,
  fg.name } into g
orderby g.Count() descending
select new TopicStatsData
{
 TopicId = g.Key.topicID,
 Count = g.Count(),
 Subject = g.Key.subject,
 ForumId = g.Key.forumID,
 ForumName = g.Key.forumName,
 ForumGroupName = g.Key.name
});

The SQL I ended up with looks kind of awful but probably the SQL engine does something like this “under the hood” on regular SQL group by to. At least it is kind of fast. The big difference in the generated SQL is that it has replaced all “LEFT OUTER JOINS” with “INNER JOINS” and they are of course faster.

Please leave a comment.

Categories: JavascriptTagged: .net core mvc interview questions, .net core vs .net mvc, angularjs asp net mvc, asp .net core mvc, asp net core mvc modal popup partial view, asp net mvc 3, asp net mvc 3.0, asp net mvc authentication, asp net mvc crud, asp net mvc dependency injection, asp net mvc development, asp net mvc generate pdf from view, asp net mvc grid, asp net mvc interview questions and answers for experienced, asp net mvc interview questions and answers pdf, asp net mvc pdf, asp net mvc projects, asp net mvc route, asp net mvc session management, asp net mvc shopping cart example, asp net mvc tutorial step by step pdf, asp net mvc upload file, asp. net mvc interview questions, dot net mvc interview questions, hire asp .net mvc developer, mvc .net core, mvc dot net, mvc example in asp net, net mvc architecture, professional asp net mvc 5

Post navigation

Previous Previous post: Using JQuery Validation plugin with ASP.NET
Next Next post: Group By with LINQ

Related Posts

  • What’s the story? JavaScript’s 30!

    #​764 — December 5, 2025 Read on the Web JavaScript Weekly 🎉  JavaScript Turns 30 Years Old  🎉 Back in May 1995, a 33 year old Brendan Eich built the first prototype of JavaScript in just ten days, originally codenamed Mocha (and then LiveScript). On December 4, 1995, Netscape and Sun Microsystems officially announced ‘JavaScript’ in a

    Posted by Posted on December 5, 2025
    0
  • Comparing performance across Node versions and ARM vs x86

    #​603 — December 2, 2025 Read on the Web Tinybench 6.0: A Tiny, Simple Benchmarking Library — Uses whatever precise timing capabilities are available (e.g. process.hrtime or performance.now). You can then benchmark whatever functions you want, specify how long or how many times to benchmark for, and get a variety of stats in return –

    Posted by Posted on December 2, 2025
    0
  • Algorithms visualized and demonstrated in JavaScript

    #​763 — November 28, 2025 Read on the Web JavaScript Weekly Over 150 Algorithms and Data Structures Demonstrated in JS — Examples of many common algorithms (e.g. bit manipulation, Pascal’s triangle, Hamming distance) and data structures (e.g. linked lists, tries, graphs) with explanations. Available in eighteen other written languages too. Oleksii Trekhleb et al. TypeScript: From

    Posted by Posted on November 28, 2025
    0
  • Guess who’s back, back again? Shai-Hulud.

    #​602 — November 25, 2025 Read on the Web How a Summer in Abruzzo Helped Bring Type Stripping to Node.js — Node.js TSC member and committer Marco tells the personal tale of what it took to bring type stripping (now considered stable) to Node. It’s neat to get the back story. He’s now working on

    Posted by Posted on November 25, 2025
    0
  • A significant Angular release

    #​762 — November 21, 2025 Read on the Web JavaScript Weekly Google Announces Angular v21 — The Google team has gone all out with this significant release of its popular JavaScript framework. They’ve put together a retro game-themed adventure-based tour of what’s new, along with top notch videos showing off features like its new signal-based

    Posted by Posted on November 21, 2025
    0
  • Did you know Node has a ‘deprecate’ method?

    #​601 — November 18, 2025 Read on the Web Node.js v25.2.1 (Current) Released (and 25.2.0 with Type Stripping Marked ‘Stable’) — v25.2.0 was released hours after we hit send last week (often the way!) and marked type stripping as stable, meaning all major server-side runtimes now support TypeScript officially (at least in type-stripping form). v25.2.1,

    Posted by Posted on November 18, 2025
    0
Judy Alvarez

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Codepolice

  • Github
  • Atlassian
  • Flatlogic
  • Xero
  • Jetbrains
  • Figma
  • What’s the story? JavaScript’s 30!
  • Comparing performance across Node versions and ARM vs x86
  • Algorithms visualized and demonstrated in JavaScript
  • Guess who’s back, back again? Shai-Hulud.
  • A significant Angular release
https://flatlogic.com/generator
COPYRIGHT © 2025 - Codepolice