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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:38:30+00:00 2026-05-28T15:38:30+00:00

I use ((ObjectQuery)IQueryable).ToTraceString() to obtain and tweak SQL code that is going to be

  • 0

I use ((ObjectQuery)IQueryable).ToTraceString() to obtain and tweak SQL code that is going to be executed by LINQ.

My problem is that unlike most IQueryable methods IQueryable.Count as defined like this:

    public static int Count(this IQueryable source) {
        return (int)source.Provider.Execute(
            Expression.Call(
                typeof(Queryable), "Count",
                new Type[] { source.ElementType }, source.Expression));
    }

executes query without compiling and returning IQueryable.
I wanted to do the trick by something like this:

public static IQueryable CountCompile(this IQueryable source) {
    return source.Provider.CreateQuery(
        Expression.Call(
            typeof(Queryable), "Count",
            new Type[] { source.ElementType }, source.Expression));
}

But then CreateQuery gives me the following exception:

LINQ to Entities query expressions can only be constructed from instances that implement the IQueryable interface.

  • 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-28T15:38:30+00:00Added an answer on May 28, 2026 at 3:38 pm

    Here’s an actual working answer I came up with when I tried to so the same. The exception says “can only be constructed from instances that implement the IQueryable interface”, so the answer seems simple: return a queryable something. Is that possible when returning a .Count()? Yes!

    public partial class YourObjectContext
    {
        private static MethodInfo GetMethodInfo(Expression<Action> expression)
        {
            return ((MethodCallExpression)expression.Body).Method;
        }
        public IQueryable<TResult> CreateScalarQuery<TResult>(Expression<Func<TResult>> expression)
        {
            return QueryProvider.CreateQuery<TResult>(
                Expression.Call(
                    method: GetMethodInfo(() => Queryable.Select<int, TResult>(null, (Expression<Func<int, TResult>>)null)),
                    arg0: Expression.Call(
                        method: GetMethodInfo(() => Queryable.AsQueryable<int>(null)),
                        arg0: Expression.NewArrayInit(typeof(int), Expression.Constant(1))),
                    arg1: Expression.Lambda(body: expression.Body, parameters: new[] { Expression.Parameter(typeof(int)) })));
        }
    }
    

    To use it:

    var query = context.CreateScalarQuery(() => context.Entity.Count());
    MessageBox.Show(((ObjectQuery)query).ToTraceString());
    

    Basically, what this does is wrap a non-IQueryable query in a subselect. It transforms the query into

    from dummy in new int[] { 1 }.AsQueryable()
    select context.Entity.Count()
    

    except lets the context’s QueryProvider handle the query. The generated SQL is pretty much what you should expect:

    SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Entity] AS [Extent1]
    )  AS [GroupBy1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following LINQ query that I need to translate to Entity SQL
We use a data acquisition card to take readings from a device that increases
I use this dynamic LINQ library together with Linq-to-Entities. I build query and after
I have been trying to use dynamic LINQ to Entity in my application for
How to use ObjectContext and ObjectQuery<T> with my own classes and objects? I.e. I
I'm using ObjectQuery.CreateQuery() to create eSQL query. I want to use the ObjcetQuery.Include to
I'm looking for an ORM that offers complete or near-complete LINQ support. LINQ to
use case example I have a servlet that is receiving login requests. If a
I am using the Code-First approach with EF and I wanted to use IDbSet
I have an ObjectQuery like that: ObjectQuery<Car> query = GetCarQuery(); than i want to

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.