4

给定以下数据:

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

我想按给定年份的最大人口对国家进行排序,然后显示该国家/地区的所有年份,然后再转到下一个国家/地区。

例如

  1. 2012 - 人口顺序将是印度、美国、英国、德国。所以我希望数据按所有印度数据、所有美国数据、所有英国数据和德国数据排序。

  2. 2002 - 人口顺序是印度、美国、德国,然后是英国。英国是最后一个,因为它没有 2002 年的数据。

我想使用 LINQ 来实现这一点,尽管我过去使用过 LINQ,但我一直在努力解决这个问题。任何帮助将非常感激。

4

3 回答 3

4

您需要按国家/地区对数据进行分组,然后按特定年份的人口排序;

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

即使给定年份没有数据,上述代码也将起作用

这可能有点难以阅读,因此您可以将 orderby 中的逻辑分离到委托中,例如;

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());
于 2012-10-03T15:54:37.960 回答
0

这与 saj 的答案相当,但使用查询语法:

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;
于 2012-10-03T20:36:24.733 回答
-1

这里有一个类似的问题: Multiple "order by" in LINQ

像这样的东西可以解决你的问题。我在 VS 中对其进行了测试,它按预期工作:

var countries order= countries.OrderBy(c => c.Year).ThenBy(n => n.population);
于 2012-10-03T16:02:23.273 回答