合并两个列表,但选择一个具有更高属性值的列表c#

时间:2016-07-08 21:09:35

标签: c# linq

我有两个对象列表

public class Items  
{
    public string Country;
    public string State;
    public string City;
    public DateTime date;
    public int population;
}

...

var items1 = new List<Items>
{
    new Item {Country="USA", State="Washington",City="Seattle",date="7/8/2016",population=1000},
    new Item {Country="USA", State="Washington",City="Seattle",date="10/8/2016",population=2000},
    new Item {Country="USA", State="Washington",City="Seattle",date="12/8/2016",population=3000},
    new Item {Country="Canada", State="Ontario",City="Washington",date="10/8/2016",population=3000},
};


var items2 = new List<Items>
{
   new Item {Country="USA", State="Washington",City="Seattle",date="10/8/2016",population=2500},
    new Item {Country="USA", State="Washington",City="Seattle",date="12/8/2016",population=2400},
    new Item {Country="Canada", State="Ontario",City="Washington",date="10/8/2016",population=3500},
};

我希望将这两个列表与国家,州城市和日期相匹配,但选择两个人口中的较高者。

items1.Concat(items2) ...linq or lambda

导致

 {
    new Item {Country="USA", State="Washington",City="Seattle",date="7/8/2016",population=1000},
    new Item {Country="USA", State="Washington",City="Seattle",date="10/8/2016",population=2500},
    new Item {Country="USA", State="Washington",City="Seattle",date="12/8/2016",population=3000},
    new Item {Country="Canada", State="Ontario",City="Washington",date="10/8/2016",population=3500},
};

2 个答案:

答案 0 :(得分:1)

您可以连接两个列表,然后按Country进行分组,然后按population降序排列每个组,并获取第一个有序元素:

        var items1 = new List<Item>
        {
            new Item {Country="USA", State="Washington",City="Seattle",population=1000},
            new Item {Country="USA", State="Washington",City="Seattle",population=2000},
            new Item {Country="USA", State="Washington",City="Seattle",population=3000},
            new Item {Country="Canada", State="Ontario",City="Washington",population=3000},
        };


        var items2 = new List<Item>
        {
            new Item {Country="USA", State="Washington",City="Seattle",population=1000},
            new Item {Country="USA", State="Washington",City="Seattle",population=2500},
            new Item {Country="USA", State="Washington",City="Seattle",population=2400},
            new Item {Country="Canada", State="Ontario",City="Washington",population=3500},
        };

        var items = items1.Concat(items2).GroupBy(i => i.Country).ToList();

        foreach (var item in items)
        {
            Item itemWithHighestPopulation = item.OrderByDescending(i => i.population).FirstOrDefault();

            // Never concatenate strings with '+', I'm just using it for testing purposes
            Console.WriteLine(itemWithHighestPopulation.City + " " + itemWithHighestPopulation.Country + " " + itemWithHighestPopulation.population);
        }

答案 1 :(得分:1)

GroupBy可能是解决这个问题最棘手的部分。一旦你过去了,在每个分组中获得你想要的那个的各种方法都可以工作。这是一个。

我们首先对这四个字段进行匿名分组。然后我们枚举每个组,按顺序对它的值进行排序,然后选择第一个结果。

USA, Washington, Seattle, 7/8/2016 12:00:00 AM, 1000
USA, Washington, Seattle, 10/8/2016 12:00:00 AM, 2500
USA, Washington, Seattle, 12/8/2016 12:00:00 AM, 3000
Canada, Ontario, Washington, 10/8/2016 12:00:00 AM, 3500

结果

self