Group order using LINQ

Given the following data:

List<Country> countries = new List<Country>(); countries.Add(new Country{ Name = "USA", population=500000, Year=2012 }); countries.Add(new Country{ Name = "USA", population=300000, Year=2002 }); countries.Add(new Country{ Name = "USA", population=250000, Year=1992 }); countries.Add(new Country{ Name = "USA", population=20000, Year=1982 }); countries.Add(new Country{ Name = "India", population=1500000, Year=2012 }); countries.Add(new Country{ Name = "India", population=1000000, Year=2002 }); countries.Add(new Country{ Name = "India", population=50000, Year=1982 }); countries.Add(new Country{ Name = "India", population=80000, Year=1992 }); countries.Add(new Country{ Name = "Germany", population=100000, Year=2012 }); countries.Add(new Country{ Name = "Germany", population=400000, Year=2002 }); countries.Add(new Country{ Name = "Germany", population=60000, Year=1992 }); countries.Add(new Country{ Name = "Germany", population=4000, Year=1982 }); countries.Add(new Country{ Name = "UK", population=450000, Year=2002 }); countries.Add(new Country{ Name = "UK", population=50000, Year=1992 }); countries.Add(new Country{ Name = "UK", population=3000, Year=1982 }); 

I want to order the countries with the largest population for a given year, but then show all the years for that country before moving to the next country.

eg.

  • 2012 - the order of the population - India, USA, UK, Germany. Therefore, I would like the data to be ordered for all data from India, all data from the United States, all British data, and then Germany.

  • 2002 - the order of the population - India, USA, Germany, and then Great Britain. The UK is the last because it does not have data for 2002.

I want to achieve this using LINQ, although I have used LINQ in the past, I am struggling to think it through. Any help would be greatly appreciated.

+4
source share
3 answers

You need to group data by country, and then sort it by population for a specific year;

 var year = 2002; var orderedCountries = countries .GroupBy(c => c.Name) .OrderByDescending(c => c.Where(y => y.Year == year).Select(p => p.population).FirstOrDefault()) .SelectMany(c=>c) .ToList(); 

The above code will work even if there is no data for a given year

It might be a little read so you can split the orderby logic into a delegate, for example.

 var orderedCountries = countries .GroupBy(c => c.Name) .OrderByDescending(c => GetPop(c, year)) .SelectMany(c=>c) .ToList(); Func<IGrouping<string, Country>, int, long> GetPop = ((cntry, yr) => (from c in cntry where c.Year == yr select c.population).First()); 
+4
source

This is quite equivalent to saj's answer, but using the query syntax:

 var y = 2002; var ordered = from c in countries group c by c.Name into g select new { Name = g.Key, Population = ( from x in g where x.Year == y select x.population).FirstOrDefault() } into s orderby s.Population descending join f in countries on s.Name equals f.Name select f; 
0
source

There is a similar question here: Several "order by" in LINQ

Something like this would do the trick in your case. I tested it in VS and it works as expected:

 var countries order= countries.OrderBy(c => c.Year).ThenBy(n => n.population); 
-1
source

Source: https://habr.com/ru/post/1437624/


All Articles