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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:26:52+00:00 2026-05-30T00:26:52+00:00

I am creating LINQ query dynamically, and I am doing OK so far. But

  • 0

I am creating LINQ query dynamically, and I am doing OK so far.

But I am stuck where I thought I wouldn’t be. At a certain point in building that query, I need to access EnityCollection of an entity. Something like this:

Expression collection = Expression.Property(entity, typeof(EntityType).GetProperty("CollectionOfRelatedEntities"));

Then, I would call “Where” LINQ method upon that collection:

MethodCallExpression AfterWhere = Expression.Call(
                        typeof(Queryable),
                        "Where",
                        new Type[] { typeof(RelatedEntity) },
                        collection,
                        Expression.Lambda<Func<RelatedEntity, bool>>(predicate, new ParameterExpression[] { paramOfRelatedEntity }));

And normally that would work. In this case it won’t because collection is IEnumerable and I need it to be IQueryable in order “Where” to work.

I tried this:

Expression.Convert(collection, typeof(IQueryable<RelatedEntity>);

but it says unable to cast because EntityCollection doesn’t implement IQueryable.

I statically use AsQueryable to achieve what i need here, so i tried mimicking that dynamically:

Expression.Call(collection, typeof(EntityCollection<RelatedEntity>).GetMethod("AsQueryable"));

but I get null reference exception. I can’t reach it via reflection. This AsQueryable method is extension method, it is static, defined in Queryable class, so i tried:

Expression.Call(collection, typeof(Queryable).GetMethod("AsQueryable", BindingFlags.Static)); 

Same result: “Value cannot be null”.

I am reaching my limits here, and I am fresh out of ideas.

So, I am asking you:

How can I dynamically cast IEnumerable to IQueryable?

  • 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-30T00:26:54+00:00Added an answer on May 30, 2026 at 12:26 am

    Try to get the method this way:

    var method = typeof(Queryable).GetMethod(
        "AsQueryable",
        BindingFlags.Static | BindingFlags.Public, 
        null, 
        new [] { typeof(IEnumerable<RelatedEntity>)}, 
        null);
    

    Then, you should be able to construct a call to that method like this:

    Expression.Call(method, collection);
    

    The problem with your code was that BindingFlags are tricky to use. If you specify any BindingFlags – like BindingFlags.Static – then you also have to explicitly say whether you want BindingFlags.Public or BindingFlags.NonPublic.

    Then the second problem is that there are two AsQueryable methods – a generic one and a non-generic one. Providing the array of type arguments resolves that ambiguity.

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

Sidebar

Related Questions

I have a situation where a certain linq query that runs against a multi-million
I'm creating a linq query to get some data that will return a list
I want to change some properties of a LINQ query result object without creating
I'm trying to follow the instructions on Creating an IQueryable LINQ Provider , but
I am creating a gridview that will be populated based upon a linq statement,
I have the result of a linq query that's binded to a gridview. OnRowDataBound
I'm trying to write Linq query on this Products table based on FacetTypes that
I'm looking at creating a dynamic linq query where the Where operator will be
Greetings I'm looking for creating a extension method including a linq query. What i'm
I have a Linq query that returns an anonymous type, which I then use

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.