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 8845849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:49:56+00:00 2026-06-14T11:49:56+00:00

I’ve been trying to write a linq query but the groupby performance is horrifically

  • 0

I’ve been trying to write a linq query but the groupby performance is horrifically slow, so I wrote my query in SQL instead and it’s really speady but I can’t get linq pad to convert it to linq for me. Can any body help me convert this sql to Linq please:

(SELECT mm.rcount, * FROM
(SELECT m.TourID AS myId, COUNT(m.RecordType) AS rcount FROM
(
((SELECT *
FROM Bookings h
WHERE h.RecordType = 'H' AND h.TourArea like '%bull%')
union
(SELECT *
FROM Bookings t
WHERE t.RecordType = 'T' and t.TourGuideName like '%bull%'))
) m
group by m.TourID) mm
INNER JOIN Bookings b ON mm.myId= b.TourID
WHERE b.RecordType = 'H');

here’s my LINQ effort but it takes like 20 seconds to iterate over 200 records:

        var heads = from head in db.GetTable<BookingType>()
                    where head.RecordType == "H" &&
                    head.TourArea.Contains("bull")
                    select g;

        var tgs = from tourguides in db.GetTable<BookingType>()
                  where tourguides.RecordType == "T" &&
                  tourguides.TourGuideName.Contains("bull")
                  select tourguides;

        var all = heads.Union(tgs);
        var groupedshit = from r in all
                          group r by r.BookingID into g
                          select g;

        return heads;

Edit 1:
Here’s my database structure:

BookingID [PK] | TourID | RecordType | TourArea | TourGuideName | ALoadOfOtherFields

And here’s some sample data:

1 | 1 | H | Bullring | null

2 | 1 | T | null | Bulldog

3 | 2 | H | Bullring | null

4 | 2 | T | null | Bulldog

5 | 2 | T | null | bull stamp

There will only ever be a single H (head) record but could potentially have many T (tour guide) records. After the grouping if I select a new (like this question: How to use LINQ to SQL to create ranked search results?) on the .Contains(‘bull’) with a .Count() I can then get ranked searching (which is the whole point of this exercise).

Edit 2:
I’ve added in a property for search rank in the class itself to avoid the problem of then converting my results into a key/value pair. I don’t know if this is best practice but it works.

    /// <summary>
    /// Search Ranking
    /// </summary>
    public int? SearchRank { get; set; }

and then I execute a SQL query directly using linq-to-sql:

        IEnumerable<BookingType> results = db.ExecuteQuery<BookingType>
        ("(SELECT  mm.rcount AS SearchRank, b.* FROM (SELECT m.TourID AS myId, COUNT(m.RecordType) AS rcount FROM (((SELECT * FROM Bookings h WHERE h.RecordType = 'H' AND h.TourArea like '%{0}%') union (SELECT * FROM Bookings t WHERE t.RecordType = 'T' and t.TourGuideName like '%{0}%')) ) m group by m.TourID) mm INNER JOIN Bookings b ON mm.myId= b.TourID WHERE b.RecordType = 'H')", "bull");

I can add in as many ‘AND’s and ‘OR’s as I like now without Linq-to-sql going mental (the query it generated was a crazy 200 lines long!

Ranked Search viola!

  • 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. Editorial Team
    Editorial Team
    2026-06-14T11:49:57+00:00Added an answer on June 14, 2026 at 11:49 am

    Why bother converting it? You can just call the SQl you have opptimized.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms

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.