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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:18:10+00:00 2026-06-15T14:18:10+00:00

I have been fiddling with dynamic LINQ for some time now, but I have

  • 0

I have been fiddling with dynamic LINQ for some time now, but I have yet to learn its secrets.

I have an expression that I want to parse that looks like this:

"document.LineItems.Select(i => i.Credit).Sum();"

During parsing of this I reach a point where I need to call a Select function on LineItems Collection. I am using factory method of Expression.Call:

Expression.Call(
    typeof(Queryable),
    "Select",
    new Type[] { typeof(LineItem), typeof(decimal?) },
    expr,
    Expression.Lambda(expression, new ParameterExpression[] { Expression.Parameter(typeof(LineItem) }));

At this moment

expr:          document.LineItems    [ICollection<LineItem>]  
expression:    LineItem.Credit       [decimal?]

none of which is materialized yet. I am just building Expression Tree at the moment.

Now, the problem:

This Expresssion.Call throws and Exception: “No generic method ‘Select’ on type ‘System.Linq.Queryable’ is compatible with the supplied type arguments and arguments”;

I resolve it easily by looking for ‘Select’ in ‘System.Linq.Enumerable’ instead of ‘Queryable’ by changing first argument of Expression.Call.

But, that’s not quite that I want. I don’t want all LineItems hauled in only to calculate Sum(), which would obviously be case with Enumerable. I want Queryable to work.

Also, for the last part of parsing – Sum(), I also need to go with Enumerable Sum(), because Queryable Sum() throws same Exception.

I have checked MSDN, both signatures of ‘Select’ function are identical, so i really cannot see why would one work and other not.

Any help or pointers would be aprreciated.

Regards,

  • 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-15T14:18:11+00:00Added an answer on June 15, 2026 at 2:18 pm

    The problem is that LineItems is an ICollection<LineItem>, while the first parameter to Queryable.Select requires an IQueryable<LineItem>. ICollection<T> only implements IEnumerable<T> which is why it works for Enumerable.Select.

    You will need to change the type of expr to be IQueryable<LineItem>.

    You should be able to do this with the Queryable.AsQueryable method. The following function creates an expression to sum the credit property values for a given Document instance:

    public static Expression<Func<decimal?>> CreateSumLineItemsExpr(Document document)
    {
        var docExpr = Expression.Constant(document);
        var itemsExpr = Expression.Property(docExpr, "LineItems");
    
        Expression<Func<LineItem, decimal?>> selector = i => i.Credit;
        var queryableExpr = Expression.Call(typeof(Queryable), "AsQueryable", new[] { typeof(LineItem) }, itemsExpr);
        var selectExpr = Expression.Call(typeof(Queryable), "Select", new[] { typeof(LineItem), typeof(decimal?) }, queryableExpr, selector);
        var sumExpr = Expression.Call(typeof(Queryable), "Sum", null, selectExpr);
    
        return Expression.Lambda<Func<decimal?>>(sumExpr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been fiddling around with lambda expressions to learn how they work but
I have been fiddling around with server side interceptors on CXF. But is seems
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been using sonar on my local machine for a long time but
I have been fiddling with this issue all day now. I need to create
I've been fiddling with some ASP.NET MVC3 solution structures and I have settled on
This is probably painfully easy to solve but I have been fiddling with it
I have this, and i've been fiddling around with it for a while but
I have been fiddling with a few lines of code for two days now
I've been fiddling around with this for quite some time. I can't work out

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.