Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 97161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:57:06+00:00 2026-05-10T23:57:06+00:00

I have this query in LINQ to Entities. var query = (from s in

  • 0

I have this query in LINQ to Entities.

        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.Forum.forumName, s.Topic.datum, s.Topic.Forum.ForumGroup.name, s.Topic.Forum.forumID } into g                      orderby g.Count() descending                      select new TopicStatsData                      {                          TopicId = g.Key.topicID,                          Count = g.Count(),                          Subject = g.Key.subject,                          ForumGroupName = g.Key.name,                          ForumName = g.Key.forumName,                          ForumId = g.Key.forumID                      }); 

I know it is kind of an ‘Evil’ query but it is only used in a admin interface. But the SQL it generated is absolutely horrifying. Have a look at this baby.

 exec sp_executesql N'SELECT TOP (50)  [Project6].[C1] AS [C1],  [Project6].[TopicId] AS [TopicId],  [Project6].[C4] AS [C2],  [Project6].[subject] AS [subject],  [Project6].[name] AS [name],  [Project6].[forumName] AS [forumName],  [Project6].[C2] AS [C3] FROM ( SELECT      [Project5].[TopicId] AS [TopicId],      [Project5].[subject] AS [subject],      [Project5].[forumName] AS [forumName],      [Project5].[name] AS [name],      1 AS [C1],       CAST( [Project5].[forumID] AS int) AS [C2],      [Project5].[C1] AS [C3],      [Project5].[C2] AS [C4]     FROM ( SELECT          [Project4].[TopicId] AS [TopicId],          [Project4].[forumID] AS [forumID],          [Project4].[subject] AS [subject],          [Project4].[forumName] AS [forumName],          [Project4].[name] AS [name],          [Project4].[C1] AS [C1],          (SELECT              COUNT(cast(1 as bit)) AS [A1]             FROM        [dbo].[tForumStats] AS [Extent14]             LEFT OUTER JOIN [dbo].[tTopic] AS [Extent15] ON [Extent14].[TopicId] = [Extent15].[topicID]             LEFT OUTER JOIN [dbo].[tForum] AS [Extent16] ON [Extent15].[forumID] = [Extent16].[forumID]             LEFT OUTER JOIN [dbo].[tForum] AS [Extent17] ON [Extent15].[forumID] = [Extent17].[forumID]             LEFT OUTER JOIN [dbo].[tForum] AS [Extent18] ON [Extent15].[forumID] = [Extent18].[forumID]             LEFT OUTER JOIN [dbo].[tForumGroup] AS [Extent19] ON [Extent18].[forumGroupID] = [Extent19].[forumGroupID]             LEFT OUTER JOIN [dbo].[tForum] AS [Extent20] ON [Extent15].[forumID] = [Extent20].[forumID]             LEFT OUTER JOIN [dbo].[tForumGroup] AS [Extent21] ON [Extent20].[forumGroupID] = [Extent21].[forumGroupID]             WHERE ([Extent14].[LogDate] >= @p__linq__25) AND ([Extent14].[LogDate] = @p__linq__25) AND ([Extent6].[LogDate] = @p__linq__25) AND ([Extent1].[LogDate] 

I do not as anyone to explain that query but it would be great to get some tips on how to optimze the query so that it just do a simple regular join. Something like this works as fine if I write the SQL myself.

SELECT COUNT(*) AS NumberOfViews, s.topicid AS topicId, t.subject AS TopicSubject, g.[name] AS ForumGroupName, f.forumName AS ForumName  FROM tForumStats s join tTopic t on s.topicid = t.topicid join tForum f on f.forumid = t.forumid JOIN tForumGroup g ON f.forumGroupID = g.forumGroupID WHERE s.[LogDate] between @date1 AND @date2 group by s.topicid,  t.subject, f.Forumname, t.Datum, g.[name] order by count(*) desc 

Btw, i LOVE this site. Amazing design and usability! Hope it works good to get some help to 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:57:07+00:00Added an answer on May 10, 2026 at 11:57 pm

    Instead of joining all tables in group by you can join the given tables by yourself. Can you try this;

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

    ps: There may be some errors, but logically it should be correct!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq to Entities query like this one: var results = from
So I have this linq query clause. var ctx = new Context(); IQueryable<Users> consulta
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have a Linq query that looks something like this: var query = from
I have a Linq query that looks something like this: var myPosse = from
I have the following LINQ to Entities query... var results = from c in
I have a LINQ query that returns some object like this... var query =
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
I have this query that gets executed though Linq to Entities. First time the
I have the following LINQ query: var aKeyword = ACT; var results = from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.