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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:24:35+00:00 2026-05-23T04:24:35+00:00

I have a table that records distance from Google/Bing between two valid UK postcodes.

  • 0

I have a table that records distance from Google/Bing between two valid UK postcodes. As the system is used, this table is added to so that the next distance query becomes fast and I do not have to call web services to retrieve distance(s) online.

Here is the table structure:

    OID                  PostcodeA PostcodeB DistanceMeters                          DistanceMiles
    -------------------- --------- --------- --------------------------------------- ---------------------------------------
    1                    BR60PS    BT248DN   788847                                  490
    2                    BR60PS    CM201JA   64426                                   40
    3                    BR60PS    CM82AP    77640                                   48
    4                    BR60PS    CO123AX   131617                                  82
    5                    BR60PS    CT146EL   119366                                  74
    6                    BR60PS    DA110TA   29247                                   18
    7                    BR60PS    DE216AH   262570                                  163
    8                    BR60PS    DL81AB    397524                                  247
    9                    BR60PS    HG27JE    368802                                  229
    10                   BR60PS    IP121AL   144394                                  90
    11                   BR60PS    IP141AH   144183                                  90
    12                   BR60PS    IP209AH   172259                                  107

Now I have a Scalar UDF that I use through Linq-to-SQL and it is defined as follows:

ALTER FUNCTION [dbo].[GetDistanceFromCache]
(@PostcodeA VARCHAR (MAX), @PostcodeB VARCHAR (MAX))
RETURNS DECIMAL
AS
BEGIN
    DECLARE @FoundDistance AS DECIMAL;
    SELECT @FoundDistance = DistanceMiles
    FROM   AddressInfoRecordedDistance
    WHERE  (PostcodeA = @PostcodeA
                    AND PostcodeB = @PostcodeB)
                 OR (PostcodeB = @PostcodeA
                         AND PostcodeA = @PostcodeB);
    RETURN ISNULL(@FoundDistance, -1);
END

I need to know if there is a any faster SQL statement or c# linq to obtain the result? If I retrieve 800 employees and run them against 50 jobs, the system goes into a pause and if I don’t add the DBContext.GetDistanceFromCache() to the set of selects, the time taken is reduced quite significantly.

And here is the stalling query:

        var query =
                    from locum in DbContext.Locums
                    where
                     locum.IsActive == true &&
                     locum.IsAdminMarkedComplete == true &&
                     locum.IsLocumsExciteBan == false &&
                     locum.IsGPHCBan == false &&
                        filterID1.Contains(locum.OID) == false &&
                        filterID2.Contains(locum.OID) == false
                    select new {
                        LocumID = locum.OID,
                        LocumName = locum.FirstName + " " + locum.LastName,
                        locum.MobileNumber,
                        locum.Email,
                        Gender = locum.Gender ? "Male" : "Female",
                        locum.DateofBirth,
                        LocumType = locum.LocumType.Name,
                        **Distance** = DbContext.GetDistanceFromCache(_Postcode, locum.AddressInfo.Postcode),
                        Address = String.Format("{0} {1} {2} {3}",
                                             locum.AddressInfo.House.Length == 0 ? String.Empty : locum.AddressInfo.House + ", ",
                                             locum.AddressInfo.Street.Length == 0 ? String.Empty : locum.AddressInfo.Street + ", ",
                                             locum.AddressInfo.Area.Length == 0 ? String.Empty : locum.AddressInfo.Area + ", ",
                                             locum.AddressInfo.Postcode ?? String.Empty),
                        Postcode = locum.AddressInfo.Postcode,
                        City = locum.AddressInfo.City.Name,
                        County = locum.AddressInfo.City.County.Name,
                        locum.SystemUserID
                    };
  • 1 1 Answer
  • 1 View
  • 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-05-23T04:24:35+00:00Added an answer on May 23, 2026 at 4:24 am

    I think your query is slow because the DB has to be called each time the LINQ query does select new { ... }, which is as many times as you have result rows.

    I would download the data from the table AddressInfoRecordedDistance before matching the result set to it. I’m thinking something like this:

    var query = from locum in DbContext.Locums
                where ...
                select new { ... }; // Don't include GetDistanceFromCache here
    
    var airds = from a in DbContext.AddressInfoRecordedDistance
                select a;
    
    foreach (var q in query)
        q.Distance = GetDistanceFromCache(q.PostcodeA, q.PostcodeB, airds);
    

    And there you have it. Of course, you also need the GetDistanceFromCache method:

    // The SQL UDF in C# code here (psuedo code, not tested, just "C blunt")
    decimal GetDistanceFromCache(string PostcodeA, string PostcodeB, List<...> table)
    {
        return (from t in table
                where
                    (t.PostcodeA == PostcodeA && t.PostcodeB == PostcodeB) ||
                    (t.PostcodeB == PostcodeA && t.PostcodeA == PostcodeB)
                select t).FirstOrDefault().DistanceMiles;
    }
    

    Of course, if you are running all of the above a lot of times, you should cache the variable airds.

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

Sidebar

Related Questions

I have a table that records a sequence of actions with a field that
I have a table that has 8 million records, with many fields, including lat/long
I have a table that has about 1/2 million records in it. Each month
I have a MySQL table holding lots of records that i want to give
I have the classical table with expandable and collapsible records that if expanded show
I have a table that records a history of address updates, called transaction. The
I have a table and want to insert the current values of records that
I have a table with records that has delete links. Basically I followed the
I have one table that contain lots of records and I want to update
I have a table that has a processed_timestamp column -- if a record has

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.