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

  • Home
  • SEARCH
  • 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 6716951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:47:41+00:00 2026-05-26T08:47:41+00:00

I have a collection that is IEnumerable<Transaction> . Transaction has several properties such as

  • 0

I have a collection that is IEnumerable<Transaction>. Transaction has several properties such as TransactionId (Int64), PaymentMethod(string) and TransactionDate(DateTime)

I’d like to be able to accomplish this transactions.GroupBy(x => x.PaymentMethod) dynamically at run time based on whatever grouping field the user has decided to use.

I found most of the answer I’m looking for in dtb’s answer here Linq GroupBy – how to specify the grouping key at runtime?

This works well:

        var arg = Expression.Parameter( typeof( Transaction ), "transaction" );
        var body = Expression.Property( arg, "PaymentMethod" );
        var lambda = Expression.Lambda<Func<Transaction, string>>( body, arg );
        var keySelector = lambda.Compile();

        var groups = transactions.GroupBy( keySelector );

Except that I don’t know the type of the return type of the Func in Expression.Lambda<Func<Transaction, string>>. It’s string in this example, but it might be Int64, decimal, DateTime, etc. I can’t use Object as the return type because I might have value types.

I’ve been reading lots of SO posts and most of them seem to apply to IQueryable and LinqToSQL.

Using the Expression class seems like a good way to accomplish this, but is there a way to do it when I don’t know either the name or datatype of my group parameter at compile time?

I appreciate any nudge in the right direction.

Edit:

Using Polity’s solution below, I created an extension method that does what I’ve been trying to do:

    public static IEnumerable<IGrouping<object, T>> GroupBy<T>( this IEnumerable<T> items, string groupByProperty )
    {

        var arg = Expression.Parameter( typeof(T), "item" );
        var body = Expression.Convert( Expression.Property( arg, groupByProperty ), typeof( object ) );
        var lambda = Expression.Lambda<Func<T, object>>( body, arg );
        var keySelector = lambda.Compile();

        var groups = items.GroupBy( keySelector );
        return groups;
    } 

Thanks to Polity and everyone who answered!

  • 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-26T08:47:41+00:00Added an answer on May 26, 2026 at 8:47 am

    Following up on the answer of ojlovecd.

    According to the one asking he needs functionality at runtime. Generics and Runtime arent the easiest mix. That is no problem though since you can just threat the return value as an object which makes the non-generic variant of the method provided by ojlovecd something like:

    static IEnumerable<IGrouping<object,Transaction>> GroupBy(string propName) 
    { 
        List<Transaction> transactions = new List<Transaction>  
        { 
            new Transaction{ PaymentMethod="AA", SomeDateTime=DateTime.Now.AddDays(-1), SomeDecimal=1.2M, SomeInt64=1000}, 
            new Transaction{ PaymentMethod="BB", SomeDateTime=DateTime.Now.AddDays(-2), SomeDecimal=3.4M, SomeInt64=2000}, 
            new Transaction{ PaymentMethod="AA", SomeDateTime=DateTime.Now.AddDays(-1), SomeDecimal=3.4M, SomeInt64=3000}, 
            new Transaction{ PaymentMethod="CC", SomeDateTime=DateTime.Now.AddDays(2), SomeDecimal=5.6M, SomeInt64=1000}, 
        }; 
        var arg = Expression.Parameter(typeof(Transaction), "transaction"); 
        var body = Expression.Convert(Expression.Property(arg, propName), typeof(object)); 
        var lambda = Expression.Lambda<Func<Transaction, object>>(body, arg); 
        var keySelector = lambda.Compile(); 
    
        var groups = transactions.GroupBy(keySelector); 
        return groups; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a IEnumerable collection of Car objects A Car has a property: Year
I have a collection object that implements IList. Inside the collection I have used
I have a collection of classes that inherit from an abstract class I created.
I have a collection of classes that I want to serialize out to an
I have a collection of elements that I need to operate over, calling member
I have a collection of people that is paginated with will_paginate @people = Person.paginate
I have a collection of non-overlapping rectangles that cover an enclosing rectangle. What is
I have a collection of domain objects that I need to convert into another
I have a collection of unmanaged dlls with a C# wrapper around them that
I have a custom object that implements INotifyPropertyChanged. I have a collection of these

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.