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

  • Home
  • SEARCH
  • 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 6176077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:02:12+00:00 2026-05-24T00:02:12+00:00

I’m using the following LINQ to SQL query on a SQL Server Compact DB

  • 0

I’m using the following LINQ to SQL query on a SQL Server Compact DB …

from article in context.OutboundArticles
where !article.IsDeleted
select article

… which generates the following SQL:

SELECT [t0].[Id], [t0].[Text], [t0].[IsDeleted]
FROM [OutboundArticle] AS [t0]
WHERE NOT ([t0].[IsDeleted] = 1)

This would be perfectly fine if it weren’t for the fact that there’s an index on the IsDeleted column and SQL Server Compact will not use the index unless the SQL looks like this:

SELECT [t0].[Id], [t0].[Text], [t0].[IsDeleted]
FROM [OutboundArticle] AS [t0]
WHERE [t0].[IsDeleted] = CONVERT(BIT, 0)

So the question is: How do I convince LINQ to SQL to generate the “CONVERT(BIT, 0)”? I’ve already tried the following …

from article in context.OutboundArticles
where article.IsDeleted == Convert.ToBoolean(0)
select article

… but the generated SQL looks the same.

  • 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-24T00:02:13+00:00Added an answer on May 24, 2026 at 12:02 am

    After a lot of digging it seems LINQ to SQL cannot be convinced to generate the CONVERT(BIT, 0) into the query. However, it can be forced to use a parameter instead of a literal in the WHERE clause, namely by compiling the query first, as follows:

    private static string QueryCompiled(Context context)
    {
        var compiled = CompiledQuery.Compile(
            (Context c, bool isDeleted) =>
                (from article in c.OutboundArticles
                 where article.IsDeleted == isDeleted
                 select article.Text).Single());
        return compiled(context, false);
    }
    

    When we run this query, the following SQL is generated:

    SELECT [t0].[Text]
    FROM [OutboundArticle] AS [t0]
    WHERE [t0].[IsDeleted] = @p0
    -- @p0: Input Boolean (Size = 0; Prec = 0; Scale = 0) [False]
    -- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: 4.0.30319.1
    

    Note the comment, @p0 seems to be typed appropriately for SQL Server Compact to actually use the index. I’ve verified this with the program below. The program first fills a new DB with 1000000 rows and then queries it with either the compiled or the ordinary query. On my machine, the timings are obvious (average from 3 runs, first discarded):

    Ordinary query on DB with index: ~670ms

    Compiled query on DB with index: ~30ms

    In both cases the query is only executed exactly once, so the compiled query does not have any advantage from the actual compilation. Further evidence that the compiled query actually uses the index while the ordinary does not comes when we manually delete the index in the DB and then run the same queries again (average from 3 runs, first discarded):

    Ordinary query on DB without index: ~680ms

    Compiled query on DB without index: ~630ms

    using System;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    
    internal static class Program
    {
        private static void Main()
        {
            var dataFile = CreateDatabase();
    
            using (var context = new Context(dataFile))
            {
                Console.WriteLine("Executing query:");
    
                // Modify this to see the difference between compiled and uncompiled queries
                const bool compiled = true;
    
                Stopwatch watch = new Stopwatch();
                context.Log = Console.Out;
                watch.Start();
    
                if (compiled)
                {
                    Console.WriteLine("Result: " + QueryCompiled(context));
                }
                else
                {
                    Console.WriteLine("Result: " + QueryNormal(context));
                }
    
                watch.Stop();
                Console.WriteLine("Elapsed milliseconds: " + watch.ElapsedMilliseconds);
            }
        }
    
        private static string CreateDatabase()
        {
            var dataFile = Path.Combine(".", "DB.sdf");
            bool databaseExists;
    
            using (var context = new Context(dataFile))
            {
                databaseExists = context.DatabaseExists();
    
                if (!databaseExists)
                {
                    Console.WriteLine("Creating database (only done on the first run)...");
                    context.CreateDatabase();
                }
            }
    
            if (!databaseExists)
            {
                const int articleCount = 1000000;
                const int batchSize = 10000;
                var random = new Random();
    
                for (int batchStart = 0; batchStart < articleCount; batchStart += batchSize)
                {
                    using (var context = new Context(dataFile))
                    {
                        for (int number = batchStart; number < batchStart + batchSize; ++number)
                        {
                            context.OutboundArticles.InsertOnSubmit(
                                new OutboundArticle()
                                {
                                    Text = new string((char)random.Next(32, 128), random.Next(32)),
                                    IsDeleted = number != articleCount / 2
                                });
                        }
    
                        context.SubmitChanges();
                    }
                }
    
                using (var context = new Context(dataFile))
                {
                    context.ExecuteCommand(
                        "CREATE INDEX IX_OutboundArticle_IsDeleted ON OutboundArticle(IsDeleted)");
                }
            }
    
            return dataFile;
        }
    
        private static string QueryNormal(Context context)
        {
            return 
                (from article in context.OutboundArticles
                 where !article.IsDeleted
                 select article.Text).Single();
        }
    
        private static string QueryCompiled(Context context)
        {
            var compiled = CompiledQuery.Compile(
                (Context c, bool isDeleted) =>
                    (from article in c.OutboundArticles
                     where article.IsDeleted == isDeleted
                     select article.Text).Single());
            return compiled(context, false);
        }
    }
    
    [Table]
    internal sealed class OutboundArticle
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        private int Id;
    
        [Column(CanBeNull = false, DbType = "NVARCHAR(32) NOT NULL")]
        internal string Text;
    
        [Column]
        internal bool IsDeleted;
    }
    
    internal sealed class Context : DataContext
    {
        internal Table<OutboundArticle> OutboundArticles;
    
        internal Context(string fileName) : base(fileName)
        {
            this.OutboundArticles = this.GetTable<OutboundArticle>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.