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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:53:27+00:00 2026-06-02T18:53:27+00:00

I’m trying to use a CompiledQuery in LINQ to SQL (WP7, C# and a

  • 0

I’m trying to use a CompiledQuery in LINQ to SQL (WP7, C# and a SQLCE 3.5 database), but after the first use the query slows down to uncompiled speeds. I’m new to this, and I’m sure I’ve missed something obvious, but I’m not sure what.

As context, I have a fairly large database of terms (about 100,000 records), and I want to search this database. After trying various different approaches and optimisations, my queries were still very slow, hence why I considered using CompileQuery.

Below is some code I threw together in LINQPad:

// A list of search terms
List<string> keywords = new List<string>()
{
    "almond",
    "banana",
    "chocolate",
    "date",
    "elderberry",
};

// Searches for each keyword in the database
void Main()
{
    int i = 0;

    while (i < keywords.Count)
    {
        Stopwatch timer = Stopwatch.StartNew();

        IQueryable<Result> r = CQ(this, keywords[i]);

        timer.Stop();

        Console.WriteLine("Query: {0}\nTime: {1}ms\n",
            query,
            timer.ElapsedMilliseconds);

        i++;
    }
}

// The compiled query property
static Func<TypedDataContext, string, IQueryable<Result>> CQ
{
    get
    {
        return CompiledQuery.Compile<TypedDataContext, string, IQueryable<Result>>
        (
            (TypedDataContext dc, string query) =>    
            (
                from x in dc.MyTable
                where x.MyColumn.Contains(query)
                select new Result
                {
                    Something = x.MyColumn
                }
            )
        );
    }
}

// A simple class to hold the results
class Result
{
    public string Something { get; set; }
}

Of course this is overly simplified, but you get the idea. Now the results produced are:

Query: almond
Time: 14ms

Query: banana
Time: 1197ms

Query: chocolate
Time: 1191ms

Query: date
Time: 1226ms

Query: elderberry
Time: 1201ms

What everyone says is that the first query will be slower, but subsequent queries will be faster. However in my case it’s the opposite: it looks like the first query is compiled but the latter ones are not.

I’m sure it’s something obvious, but I’m not sure what I’m missing. Any pointers?

Many thanks in advance!

  • 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-02T18:53:29+00:00Added an answer on June 2, 2026 at 6:53 pm

    Try saving the delegate result of the query compilation to a static backing field. You’re likely recompiling everytime you access your property. Not sure why the first execution is so fast. It’s not possible its something data related is it?

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.