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

  • Bringing Python apps into Node

    #​596 — October 14, 2025 Read on the Web 📂 A Modern Guide to Reading and Writing Files in Node — A comprehensive guide to various methods for working with files, from promise-based methods through to working with streams, processing files concurrently, using file handles, and memory-efficient techniques. Luciano Mammino A Way to Integrate Python ASGI with Node.js

    Posted by Posted on October 14, 2025
    0
  • Vite gets its own documentary

    #​756 — October 10, 2025 Read on the Web JavaScript Weekly ▶  Vite: The Documentary — From the same creators of the fantastic ▶️ Node.js, ▶️ Angular and ▶️ React documentaries comes an up to date look at Vite, the build tool that has taken the JavaScript ecosystem by storm in recent years. Many luminaries make an appearance to

    Posted by Posted on October 10, 2025
    0
  • npm security best practices to consider

    #​595 — October 7, 2025 Read on the Web 15 Recent Node Features That Can Replace Popular npm Packages — Many features that once required third-party packages are now built into the runtime itself. Here’s a look at some of the most notable that you may want to experiment with, prior to reducing unnecessary dependencies. Lizz

    Posted by Posted on October 7, 2025
    0
  • React 19.2 is in the building

    #​755 — October 3, 2025 Read on the Web JavaScript Weekly The State of JavaScript 2025 Survey — Each year, Devographics runs an epic survey of as many JavaScript community members as it can and turns the results into an interesting report on the state of the ecosystem – here’s the results from 2024. If

    Posted by Posted on October 3, 2025
    0
  • Using Node with Cloudflare Workers

    #​594 — September 30, 2025 Read on the Web 🗓️ We’re back after taking a week off for my birthday. I’ve never bothered to do that before, but I figured I’d give it a go, and.. it was good 😅 We’re now back every week until Christmas!__Peter Cooper, your editor A Year of Improving Node.js

    Posted by Posted on September 30, 2025
    0
  • The first browser with JavaScript landed 30 years ago

    #​754 — September 26, 2025 Read on the Web JavaScript Weekly Give Your AI Eyes: Introducing Chrome DevTools MCP — The Chrome team has released an MCP server for Chrome DevTools, enabling agents like Claude Code or OpenAI Codex to use the DevTools to debug and analyze the performance and behavior of your webapps (or

    Posted by Posted on September 26, 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
  • Bringing Python apps into Node
  • Vite gets its own documentary
  • npm security best practices to consider
  • React 19.2 is in the building
  • Using Node with Cloudflare Workers
https://flatlogic.com/generator
COPYRIGHT © 2025 - Codepolice