Home > Blockchain >  Creating new data type and grouping by it LINQ
Creating new data type and grouping by it LINQ

Time:12-02

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?

CodePudding user response:

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
    });

CodePudding user response:

new YearSchoolStat {NumberOfSchools = 1, Year = 2017},
new YearSchoolStat {NumberOfSchools = 2, Year = 2018},
new YearSchoolStat {NumberOfSchools = 4, Year = 2019}

the following code will give the above output

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