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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:45:06+00:00 2026-05-27T02:45:06+00:00

I use asp.net 4 c# and ef4. I have this code, it should compile

  • 0

I use asp.net 4 c# and ef4.

I have this code, it should compile a query and return a single scalar value (I use anonymous type).

My code does not have apparently errors, but because is the first time I write a compiled query I would like to know if is well written or could be improved for a performance boost.

   var query = CompiledQuery.Compile((CmsConnectionStringEntityDataModel ctx)
                        => from o in ctx.CmsOptions
                           where o.OptionId == 7
                           select new
                           {
                               Value = o.Value
                           });

                    uxHtmlHead.Text = query(context).FirstOrDefault().Value;// I print the result in a Label

SQL Profile Output:

SELECT TOP (1) 
[Extent1].[OptionId] AS [OptionId], 
[Extent1].[Value] AS [Value]
FROM [dbo].[CmsOptions] AS [Extent1]
WHERE 7 = [Extent1].[OptionId]

Many Thanks


Result after Wouter advice (please guys have a double check again):

        static readonly Func<CmsConnectionStringEntityDataModel, int, string> compiledQueryHtmlHead =
    CompiledQuery.Compile<CmsConnectionStringEntityDataModel, int, string>(
            (ctx, id) => ctx.CmsOptions.FirstOrDefault(o => o.OptionId == id).Value);


using (var context = new CmsConnectionStringEntityDataModel())
            {
                int id = 7;
                uxHtmlHead.Text = compiledQueryHtmlHead.Invoke(context, id);
            }

Resulting SQL (I do not understand why with a LEFT JOIN)

exec sp_executesql N'SELECT 
[Project1].[Value] AS [Value]
FROM   ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN  (SELECT 
    [Extent1].[Value] AS [Value]
    FROM [dbo].[CmsOptions] AS [Extent1]
    WHERE [Extent1].[OptionId] = @p__linq__0 ) AS [Project1] ON 1 = 1',N'@p__linq__0 int',@p__linq__0=7
  • 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-27T02:45:07+00:00Added an answer on May 27, 2026 at 2:45 am

    There are 2 things you can improve on.

    First, precompiling a query is definitely a good idea but if you have a look at your code you will see that it precompiles the query each and every time instead of only once.

    You need to move the precompiled query to a static variable that is initialized only once.

    Another thing you need to be careful of is that when precompiling a query you shouldn’t modify the query anymore before executing it.

    You are building a precompiled query that will select all rows and then you say ‘firstordefault’ which changes the precompiled query to a SELECT TOP (1) and you lose the benefit of precompiling. You need to move the FirstOrDefault part inside your precompiled query and return only one result.

    Have a look at this documentation. If you look at the examples you can see how they use a static field to hold the compiled query and how they specify the return value.

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

Sidebar

Related Questions

I use c# asp-net 4 and ef4. I have a LINQ query which create
I use asp.net, c# and EF4. I'm puzzled by this LINQ query: var queryContents
I use Asp.Net 4 and C#, I use EF 4. I have this query,
I use LINQ, C#, EF4. I have this query expression in Linq. I need
May be this is too trivial question. I use C#, asp.net 4, EF4, I
We use ASP.NET / C#. We work locally, test locally, check in our code
I use ASP.NET and have a label control on my page, which I fill
I use ASP.NET and have a Button and a CustomValidator, which has to validate
I have tried to use ASP.NET MVC for a while, then I face a
We have an ASP.Net MVC2 web site, and are utilizing EF4 for database access,

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.