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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:40:47+00:00 2026-05-31T15:40:47+00:00

I have a first draft for an extension method on IQueryable with an override

  • 0

I have a first draft for an extension method on IQueryable with an override for GroupBy which should group by a named property.

Before you ask, I’m not using the Dynamic Expression API aka Dynamic Linq because I couldn’t figure out how to access a count for each group with that. The GroupBy method in that library returns a non-generic IQueryable, rather than an IQueryable<IGrouping<>> that I need for my counts. If there is a way to get at the counts through that library, I’ll be happy to accept an answer showing me how. Perhaps some sort of syntax that I couldn’t find the documentation for in the string elementSelector parameter?

The code:

    public static IQueryable<IGrouping<object, T>> GroupBy<T>(this IQueryable<T> source, string propertyName)
    {
        ParameterExpression param = Expression.Parameter(typeof(T), String.Empty);
        MemberExpression property = Expression.PropertyOrField(param, propertyName);
        LambdaExpression group = Expression.Lambda(property, param);

        MethodCallExpression call = Expression.Call(
            typeof(Queryable),
            "GroupBy",
            new[] { typeof(T), property.Type },
            source.Expression,
            Expression.Quote(group));

        return source.Provider.CreateQuery<IGrouping<object, T>>(call);
    }

I consume it like this:

    public IEnumerable<Category> GetCategories(IQueryable<T> entities, string property)
    {
        var haba = entities.GroupBy(property);
        var categories = haba.Select(group => new Category(group.Key.ToString(), group.Count()));
        return categories.ToArray();
    }

And it workes fine as long as my property is of type string. However, if I attempt to use it on an int or bool property I get something like the following error on the call to CreateQuery:

Argument expression has type
System.Linq.IQueryable1[System.Linq.IGrouping2[System.Int32,DerivedEntity1]]
when type
System.Collections.Generic.IEnumerable1[System.Linq.IGrouping2[System.Object,DerivedEntity1]]
was expected. Parameter name: expression

I notice that it’s expecting an IEnumerable generic rather than an IQueryable, which is strange. But a pretty good tip. You can’t cast IEnumerable to an IQueryable without doing an AsQueryable(). The question then is, why is it giving me an IEnumerable instead of an IQueryable? An why isn’t it failing in the same way when I pass in a propertyName for a string property?

I should mention that my IQueryable‘s are instances of NhQueryable, from NHibernate, that is.

  • 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-31T15:40:48+00:00Added an answer on May 31, 2026 at 3:40 pm

    I think boxing from value types is missing from your code:

    public static IQueryable<IGrouping<object, T>> GroupBy<T>(this IQueryable<T> source, string propertyName)
    {
        ParameterExpression param = Expression.Parameter(typeof(T), String.Empty);
        MemberExpression property = Expression.PropertyOrField(param, propertyName);
        UnaryExpression convert = Expression.Convert(property, typeof(object));
        LambdaExpression group = Expression.Lambda(convert, param);
    
        MethodCallExpression call = Expression.Call(
            typeof(Queryable),
            "GroupBy",
            new[] { typeof(T), typeof(object) },
            source.Expression,
            Expression.Quote(group));
    
        return source.Provider.CreateQuery<IGrouping<object, T>>(call);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my first professional assignment of making a website in which a photographer's
I have an Entity Framework 4 model-first design. I create a first draft of
I almost finished writing the first draft of my game's logic engine which is
A bit long, but, shorter than the first draft :) We have a small
i have created a project which has different xib viewcontrollers.In first view am selecting
I have First/Last/Previous/Next buttons that change the selected child node of a TreeViewItem. Setting
I have first: select url from urls where site = '$value' order by url
I have first names stored in one table and last names stored in another.
I have my first ever interview for a Java developer role, specifically RMI, Serverlets
Once you have your first set of requirements and design done where do you

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.