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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:01:30+00:00 2026-05-24T13:01:30+00:00

Is there any performance issue to be aware about using this sql statement generated

  • 0

Is there any performance issue to be aware about using this sql statement generated by EF

C# code:

 public IQueryable<Lugar> NearestPOI(double lat, double lng, int> distance)
            {
                System.Data.Objects.ObjectResult<Int32?> AllowedPois = dbContext.SP_NearestPOI(lat, lng, 100000);


                IQueryable<Lugar> POI = from c in dbContext.Lugars
                          where AllowedPois.Contains(c.id)
                          select c;

                return POI;
            }

EF4 Generated query:

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[empresaId] AS [empresaId], 
[Extent1].[usuarioId] AS [usuarioId], 
[Extent1].[name] AS [name], 
[Extent1].[description] AS [description], 
[Extent1].[lat] AS [lat], 
[Extent1].[lng] AS [lng],
[Extent1].[logoThumbnail] AS [logoThumbnail], 
[Extent1].[imageType] AS [imageType]
FROM [dbo].[Lugares] AS [Extent1]
WHERE [Extent1].[id] IN (1,2,3,4,5,6,7)

My concern is about doing the AllowedPois query as a separate one instaed of using the regular approach on pure SQL syntax that would be something like:

SELECT * from dbo.Lugares  L join dbo.NearestPOI(9.105306627167566,-79.38148587942118,100000) NL
on L.id = NL.id

As im using EF4 for this project I would like to stick with it and do not use string concatenation for the querys. I tried to generate a more compelling query using this approach:

var POI = from c in dbContext.Lugars
          join i in dbContext.SP_NearestPOI(lat, lng, 100000) on c.id  equals i.Value
          select c;

But it give out a really messy query with N number of unions that increments with the number of allowedPois:

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[empresaId] AS [empresaId], 
[Extent1].[usuarioId] AS [usuarioId], 
[Extent1].[name] AS [name], 
[Extent1].[description] AS [description], 
[Extent1].[lat] AS [lat], 
[Extent1].[lng] AS [lng], 
[Extent1].[logoThumbnail] AS [logoThumbnail], 
[Extent1].[imageType] AS [imageType]
FROM  [dbo].[Lugares] AS [Extent1]
INNER JOIN  (SELECT 
    [UnionAll5].[C1] AS [C1]
    FROM  (SELECT 
        [UnionAll4].[C1] AS [C1]
        FROM  (SELECT 
            [UnionAll3].[C1] AS [C1]
            FROM  (SELECT 
                [UnionAll2].[C1] AS [C1]
                FROM  (SELECT 
                    [UnionAll1].[C1] AS [C1]
                    FROM  (SELECT 
                        1 AS [C1]
                        FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
                    UNION ALL
                        SELECT 
                        2 AS [C1]
                        FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]
                UNION ALL
                    SELECT 
                    3 AS [C1]
                    FROM  ( SELECT 1 AS X ) AS [SingleRowTable3]) AS [UnionAll2]
            UNION ALL
                SELECT 
                4 AS [C1]
                FROM  ( SELECT 1 AS X ) AS [SingleRowTable4]) AS [UnionAll3]
        UNION ALL
            SELECT 
            5 AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable5]) AS [UnionAll4]
    UNION ALL
        SELECT 
        6 AS [C1]
        FROM  ( SELECT 1 AS X ) AS [SingleRowTable6]) AS [UnionAll5]
UNION ALL
    SELECT 
    7 AS [C1]
    FROM  ( SELECT 1 AS X ) AS [SingleRowTable7]) AS [UnionAll6] ON [Extent1].[id] = [UnionAll6].[C1]

Any idea on how to improve this operation or should I stick with my actual solution using the separate query for the allowedPois?

  • 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-05-24T13:01:31+00:00Added an answer on May 24, 2026 at 1:01 pm

    You really shouldn’t use the join syntax in a LINQ query. Part of me wishes they didn’t include the join operator in the C# language for LINQ because it just leads to confusion and tends to generate bad SQL like this. The reason being is that you almost are always joining in SQL to when you want to do a where clause or include that table in your results. LINQ already has great support for this but you just have to think in the LINQ way and not the SQL way. Your LINQ data model should have the relationships defined so you don’t then need to redefine them through joins. Doesn’t totally apply here since you’re using a sproc first but same principles apply. Your first LINQ query in C# is actually the efficient way to do it.

    So, in case I wasn’t clear. This is the right code.

    public IQueryable<Lugar> NearestPOI(double lat, double lng, int> distance)
                {
                    System.Data.Objects.ObjectResult<Int32?> AllowedPois = dbContext.SP_NearestPOI(lat, lng, 100000);
    
    
                    IQueryable<Lugar> POI = from c in dbContext.Lugars
                              where AllowedPois.Contains(c.id)
                              select c;
    
                    return POI;
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any performance advantage to using lists over dictionaries over tuples in Python?
Is there any performance gain using a CTE over a derived table?
Is there any performance penalty for the following code snippet? for (int i=0; i<someValue;
Are there any performance benefits to me not using the gridview in asp.net for
is there any memory of performance issues i need to be aware of when
we have some performance issue with a program, I traced tsql using sql server
Are there any performance issues with registering a number of routes with the routing
Is there any performance difference between tuples and lists when it comes to instantiation
Is there any performance to be gained these days from compiling java to native
Is there any performance difference between the for loops on a primitive array? Assume:

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.