Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ: Get items with max price

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" }
};
like image 508
Lari13 Avatar asked May 25 '11 11:05

Lari13


People also ask

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in simple words?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

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));
like image 142
Jon Skeet Avatar answered Sep 25 '22 08:09

Jon Skeet


var ret = list.GroupBy(x => x.Title)
              .Select(g => g.Aggregate((a, x) => (x.Price > a.Price) ? x : a));

(And if you need the results to be a List<T> rather than an IEnumerable<T> sequence then just tag a ToList call onto the end.)

like image 25
LukeH Avatar answered Sep 21 '22 08:09

LukeH