Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating new data type and grouping by it LINQ

A sequence of data about applicants nameList of type Entrant is given. Each element of the sequence includes the fields School number, Year of entering, Last name. Get data (list of YearSchoolStat values) about the number of different schools that applicants graduated from for each year present in the source data. The YearSchoolStat type includes the Year of entering, Number of Schools fields. The list of YearSchoolStat values must be sorted in ascending order of the number of schools, and for matching values, in ascending order of the year number. Example of data provided and expected results:

            nameList: new[]
            {
                new Entrant {LastName = "Name", SchoolNumber = 12, Year = 2019},
                new Entrant {LastName = "Name", SchoolNumber = 12, Year = 2019},
                new Entrant {LastName = "Name", SchoolNumber = 13, Year = 2019},
                new Entrant {LastName = "Name", SchoolNumber = 14, Year = 2019},
                new Entrant {LastName = "Name", SchoolNumber = 15, Year = 2019},
                new Entrant {LastName = "Name", SchoolNumber = 12, Year = 2018},
                new Entrant {LastName = "Name", SchoolNumber = 12, Year = 2018},
                new Entrant {LastName = "Name", SchoolNumber = 13, Year = 2018},
                new Entrant {LastName = "Name", SchoolNumber = 12, Year = 2017},
                new Entrant {LastName = "Name", SchoolNumber = 12, Year = 2017}
            },
            expected: new[]
            {
                new YearSchoolStat {NumberOfSchools = 1, Year = 2017},
                new YearSchoolStat {NumberOfSchools = 2, Year = 2018},
                new YearSchoolStat {NumberOfSchools = 4, Year = 2019}
            });

I’m trying to group by SchoolNumber and Year and then for the number of schools I want to use something like Count() but it’s not permitted.

var result = nameList.GroupBy(c => new
            {
                c.SchoolNumber,
                c.Year,
            }).Select(ss => new YearSchoolStat()
            {
                Year = ss.Key.Year,
                NumberOfSchools = ss.Key.SchoolNumber

            });

What is wrong with my approach and what else I should try?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If we want to group by year, let us group it by year only. That ‘ll help us to count the result subsets:

var result = nameList
    .GroupBy(c => c.Year)
    .Select(ss => new YearSchoolStat()
    {
        Year = ss.Key,
        NumberOfSchools = ss.Count() // Here is the magic
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading