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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:52:09+00:00 2026-05-22T18:52:09+00:00

I have a list of my objects: class MyObj { public String Title {

  • 0

I have a list of my objects:

class MyObj
{
    public String Title { get; set; }
    public Decimal Price { get; set; }
    public String OtherData { get; set; }
}

var list = new List<MyObj> {
 new MyObj { Title = "AAA", Price = 20, OtherData = "Z1" },
 new MyObj { Title = "BBB", Price = 20, OtherData = "Z2" },
 new MyObj { Title = "AAA", Price = 30, OtherData = "Z5" },
 new MyObj { Title = "BBB", Price = 10, OtherData = "Z10" },
 new MyObj { Title = "CCC", Price = 99, OtherData = "ZZ" }
};

What is the best way to get list with unique Title and MAX(Price).
Resulting list needs to be:

var ret = new List<MyObj> {
 new MyObj { Title = "BBB", Price = 20, OtherData = "Z2" },
 new MyObj { Title = "AAA", Price = 30, OtherData = "Z5" },
 new MyObj { Title = "CCC", Price = 99, OtherData = "ZZ" }
};
  • 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-22T18:52:10+00:00Added an answer on May 22, 2026 at 6:52 pm

    Well, you could do:

    var query = list.GroupBy(x => x.Title)
                    .Select(group =>
                    {
                        decimal maxPrice = group.Max(x => x.Price);
                        return group.Where(x => x.Price == maxPrice)
                                    .First();
                    };
    

    If you need LINQ to SQL (where you can’t use statement lambdas) you could use:

    var query = list.GroupBy(x => x.Title)
           .Select(group => group.Where(x => x.Price == group.Max(y => y.Price))
                                 .First());
    

    Note that in LINQ to Objects that would be less efficient as in each iteration of Where, it would recompute the maximum price.

    Adjust the .First() part if you want to be able return more than one item with a given name if they both have the same price.

    Within LINQ to Objects you could also use MoreLINQ‘s MaxBy method:

    var query = list.GroupBy(x => x.Title)
                    .Select(group => group.MaxBy(x => x.Price));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects called Activity: class Activity { public Date activityDate;
If I have a list of objects called Car: public class Car { public
i have a list of lat/long objects on my server. eg. public class LatitudeLongitude
Say I have a list of Person objects: class person { int id; string
okay, I have a list of class objects like so: List<fruit> lst = new
I have a simple class like this: public class myClass { public List<string> innerList;
I have a stl::list containing Widget class objects. They need to be sorted according
I have a class that contains a list of objects. What's the best way
I have class A and list of A objects. A has a function f
I have a class Player that contains a list of Accessory objects. There are

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.