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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:02:28+00:00 2026-05-24T17:02:28+00:00

My application frequently needs to group a table, then return the row with the

  • 0

My application frequently needs to group a table, then return the row with the maximum value for that group. This is pretty easy to do in LINQ:

myTable.GroupBy(r => r.FieldToGroupBy)
.Select(r => r.Max(s => s.FieldToMaximize))
.Join(
    myTable,
    r => r,
    r => r.FieldToMaximize,
    (o, i) => i)

Now suppose I want to abstract this out into its own method. I tried writing this:

public static IQueryable<TSource>
SelectMax<TSource, TGroupKey, TMaxKey>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, TGroupKey>> groupKeySelector,
    Expression<Func<TSource, TMaxKey>> maxKeySelector)
    where TMaxKey : IComparable
{
    return source
        .GroupBy(groupKeySelector)
        .Join(
            source,
            g => g.Max(maxKeySelector),
            r => maxKeySelector(r),
                (o, i) => i);
}

Unfortunately this doesn’t compile: maxKeySelector is an expression (so you can’t call it on r, and you can’t even pass it to Max. So I tried rewriting, making maxKeySelector a function rather than an expression:

public static IQueryable<TSource>
SelectMax<TSource, TGroupKey, TMaxKey>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, TGroupKey>> groupKeySelector,
    Func<TSource, TMaxKey> maxKeySelector)
    where TMaxKey : IComparable
{
    return source
        .GroupBy(groupKeySelector)
        .Join(
            source,
            g => g.Max(maxKeySelector),
            r => maxKeySelector(r),
                (o, i) => i);
}

Now this compiles. But it fails at runtime: “Unsupported overload used for query operator ‘Max’.” This is what I’m stuck on: I need to find the right way to pass maxKeySelector into Max().

Any suggestions? I’m using LINQ to SQL, which seems to make a difference.

  • 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-24T17:02:29+00:00Added an answer on May 24, 2026 at 5:02 pm

    First of all, I’d like to point out that what you’re trying to do is even easier than you think in LINQ:

    myTable.GroupBy(r => r.FieldToGroupBy)
        .Select(g => g.OrderByDescending(r => r.FieldToMaximize).FirstOrDefault())
    

    … which should make our lives a little easier for the second part:

    public static IQueryable<TSource>
    SelectMax<TSource, TGroupKey, TMaxKey>(
        this IQueryable<TSource> source,
        Expression<Func<TSource, TGroupKey>> groupKeySelector,
        Expression<Func<TSource, TMaxKey>> maxKeySelector)
        where TMaxKey : IComparable
    {
        return source
            .GroupBy(groupKeySelector)
            .Select(g => g.AsQueryable().OrderBy(maxKeySelector).FirstOrDefault());
    }
    

    The key is that by making your group an IQueryable, you open up a new set of LINQ methods that can take actual expressions rather than taking Funcs. This should be compatible with most standard LINQ providers.

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

Sidebar

Related Questions

I am building a desktop application that needs to update current user's status frequently.
I'm working on a web application that needs to frequently poll the server database
My win32 C++ application frequently checks a file that normally resides on a network
I frequently start with a simple console application to try out an idea, then
We have an application containing a lot of user controls that update frequently based
I'm currently writing a Java application that needs to look at how heavily loaded
I have a command line application that needs to support arguments of the following
I'm writing an iPhone application that needs to send small bits of information (two
I'm trying to figure out what are the layers that a web application needs
In my web application I frequently have sections that need to be collapsed/expanded. I

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.