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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:19:42+00:00 2026-06-02T13:19:42+00:00

I have IQueryable< T> source and i want to dynamically call IQueryable< T>.Count(). So,

  • 0

I have IQueryable< T> source and i want to dynamically call IQueryable< T>.Count().

So, i need MethodInfo of Count method declared in IQueryable.

this is its signature (in IQueryable<>) from msdn:

public static int Count<TSource>(
    this IQueryable<TSource> source
)

This is how far i got:

Expression expr; //this is expression which holds my IQueryable<T>
MethodInfo mi = expr.Type.GetMethod("Count", BindingFlags.Static | BindingFlags.Public, null, new[] { expr.Type }, null); 

but my mi is always null;

I also tried:

mi = typeof(IQueryable<>).GetMethod("Count", BindingFlags.Static | BindingFlags.Public, null, new[] { expr.Type }, null);

but again null.

My final goal would be:

Expression.Call(mi, expr);

UPDATE:
this is how I get Sum Extension method:

MethodInfo sum = typeof(Queryable).GetMethod("Sum", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(IQueryable<decimal>) }, null);

and this works, but this Sum method is not generic. It is though static.

  • 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-02T13:19:43+00:00Added an answer on June 2, 2026 at 1:19 pm

    You need to crack out the generic argument of the IQueryable<T> type and use that; also the type that owns the method is not IQueryable<T>, it is Queryable – if you think about it – interfaces can’t have static methods (well, as a commenter has pointed out, in C# that is) :).

    Also, because it’s a generic method you can’t match the parameters in the way you’ve tried: because you need to pass the generic type definition IQuerable<TSource> – not a generic type IQueryable<int> or whatever the actual expression is.

    Instead you can just look for a single-parametered version of a static method called ‘Count’ on the Queryable type:

    Type genericArgument = expr.GetGenericArguments()[0];
    
    MethodInfo countMethod = typeof(Queryable)
                  .GetMethods(BindingFlags.Static | BindingFlags.Public)
                  //narrow the search before doing 'Single()'
                  .Single(mi => mi.Name == "Count"
                             // this check technically not required, but more future proof
                             && mi.IsGenericMethodDefinition
                             && mi.GetParameters().Length == 1)
                  .MakeGenericMethod(genericArgument);
    
    //now you can bind to 'countMethod'
    

    Update 7th March 2017 – Clearly, something changed in the framework which stopped the original version of the code example from working – this is an updated version which should work

    Going into it a bit further – the signature of the method is:

    public static int Count<TSource>(
      this IQueryable<TSource> source
    )
    

    So while the parameter type is IQueryable<TSource> it’s generic over the TSource type – hence the reason why you need to fish into your IQueryable<TSource> expression and grab it’s generic argument. And you should also be able to see what I mean about the parameter here.

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

Sidebar

Related Questions

i have this in my BlogRepository public IQueryable<Subnus.MVC.Data.Model.Post> GetPosts() { var query = from
I thought this was fixed in 4.0. I have this method public IQueryable<T> All(Expression<Func<T,object>>
I want to get the method System.Linq.Queryable.OrderyBy<T, TKey>(the IQueryable<T> source, Expression<Func<T,TKey>> keySelector) method, but
I have a domain service, derived from LinqToEntitiesDomainService<FOOEntities> It has one method, IQueryable<Bar> GetBar().
I have IQueryable list of objects of type T which I want to transform
I have an IQueryable and an object of type T. I want to do
I have a huge IQueryable that I want to export directly to Excel for
I have a method that takes an IQueryable. Is there a LINQ query that
I have an extension method to dynamically filter Linq to Entities results using string
I have a DataGridView with myGridView.DataSource = GetSomeData() // method called public IQueryable GetSomeData()

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.