-3

Having a list with data like:

{ surname = surname_1, title = title_4, release_number = 111, year = 2021 }
{ surname = surname_1, title = title_1, release_number = 111, year = 2020 }
{ surname = surname_2, title = title_5, release_number = 222, year = 2021 }
{ surname = surname_2, title = title_2, release_number = 222, year = 2020 }
{ surname = surname_3, title = title_8, release_number = 222, year = 2022 }

How can I get max year for each release_number:

{ReleaseNumber = 111, Year = 2021}
{ReleaseNumber = 222, Year = 2022}
mehrandvd
  • 8,806
  • 12
  • 64
  • 111
  • 1
    Does this answer your question? [LINQ GROUP BY and MAX()](https://stackoverflow.com/questions/10085947/linq-group-by-and-max) – Josh Mein Jun 07 '22 at 16:57
  • 2
    You should read [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/) or book for C#, for example [C# in a nutshell](https://www.oreilly.com/library/view/c-90-in/9781098100957/) – Lazar Đorđević Jun 07 '22 at 16:57
  • 2
    What you posted isn't data. It's certainly not valid C# or even JSON. What does your actual *code* look like? Have you tried using `GroupBy` or `Max` ? – Panagiotis Kanavos Jun 07 '22 at 16:57
  • 1
    @JoshMein that's not a good duplicate. That question is far more complicated and specific to EF. A developer that would understand what parts are relevant wouldn't be asking about Group By and Max in the first place – Panagiotis Kanavos Jun 07 '22 at 16:59
  • 1
    @PanagiotisKanavos Yeah I realized that after the fact, but this is still a duplicate and little effort has been shown in trying to solve the problem. I am searching for another duplicate now. – Josh Mein Jun 07 '22 at 17:02
  • This question looks similar [How do I get the MAX row with a GROUP BY in LINQ query?](https://stackoverflow.com/questions/157786/how-do-i-get-the-max-row-with-a-group-by-in-linq-query) – Lazar Đorđević Jun 07 '22 at 17:06
  • Does this answer your question? [How do I get the MAX row with a GROUP BY in LINQ query?](https://stackoverflow.com/questions/157786/how-do-i-get-the-max-row-with-a-group-by-in-linq-query) – Lazar Đorđević Jun 07 '22 at 17:12

5 Answers5

1

Considering you have a list of items you could have this LINQ query:

var releaseGroups = 
    from item in list
    group item by item.release_number into releaseGroup
    select new {ReleaseNumber = releaseGroup.Key, MaxYear = realseGroup.Select(o=>o.year).Max()};
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
mehrandvd
  • 8,806
  • 12
  • 64
  • 111
0

You need to create a model for your data then use code below

var items= new List<YourModel> {
    new YourModel { surname = surname_1, title = title_4, release_number = 111, year = 2021 },
    new YourModel { surname = surname_1, title = title_1, release_number = 111, year = 2020 },
    new YourModel { surname = surname_2, title = title_5, release_number = 222, year = 2021 },
    new YourModel { surname = surname_3, title = title_8, release_number = 222, year = 2022 },
};

var MaxByYear=
    from item in items
    group item by item.year into itemGroup
    select new
    {
        Year = itemGroup.Key,
        Max= itemGroup.Max(x => x.release_number),
    };

// MaxByYear is collection of anonymous objects:

0

LINQ syntax is roughly equivalent to SQL. To get a maximum value in a specific grouping in SQL you use the GROUP BY clause to specify the grouping, then select the maximum value in that group.

SELECT Key,MAX(SomeField)
FROM SomeTable
GROUP BY Key

All other answers show how to do this, eg :

list
  .GroupBy(i => i.release_number)
  .Select(grp => new {grp.Key, grp.Select(i => i.year).Max());

When working with an in memory list though, GroupBy has several overloads that can shorten the code to a single line :

var results=list.GroupBy(item => item.release_number,
                         item=>item.Year,
                         (key,values) => new { 
                                           Key=key,
                                           Max=values.Max()
            });

In this overload, the first argument extracts the group key. The second extracts the values we want in that group. The final argument calculates the result for each key.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
0

You need to group your data by release number and then take max year (or order by descending year and take first).

var res = testData.GroupBy(x => x.ReleaseNumber)
                .Select(x => new {ReleaseNumber = x.Key, Year = x.OrderByDescending(t => t.Year).First().Year});

Here my full example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<TestData> testData = new List<TestData>()
            {
                new TestData() {ReleaseNumber = 111, Year = 2021},
                new TestData() {ReleaseNumber = 111, Year = 2020},
                new TestData() {ReleaseNumber = 222, Year = 2021},
                new TestData() {ReleaseNumber = 222, Year = 2020},
                new TestData() {ReleaseNumber = 222, Year = 2022},
            };

            var res = testData
                .GroupBy(x => x.ReleaseNumber)
                .Select(x => new {ReleaseNumber = x.Key, Year = x.OrderByDescending(t => t.Year).First().Year});

            foreach (var r in res)
            {
                Console.WriteLine($"Release number: {r.ReleaseNumber}, Year: {r.Year}");
            }

            Console.ReadLine();
        }
    }

    public class TestData
    {
        public int ReleaseNumber { get; set; }
        public int Year { get; set; }
    }
}
-1
list
  .GroupBy(i => i.release_number)
  .Select(grp => new {grp.Key, grp.Select(i => i.year).Max()});
brent.reynolds
  • 396
  • 2
  • 12