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



我有两个对象列表

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

GroupBy 可能是解决这个问题最棘手的部分。一旦你通过了这个,在每个分组中获得你想要的方法就可以了。这是其一。

我们首先对四个字段进行匿名分组。然后,我们枚举每个组,按总体降序排列其值,并选择第一个结果。

var merged = new List<Item>(items1);
merged.AddRange(items2);
IEnumerable<Item> grouped = merged
    .GroupBy((item) => new { item.Country, item.State, item.City, item.date })
    .Select((item) => item.OrderByDescending(i => i.population).FirstOrDefault());
foreach(Item item in grouped)
{
    Console.WriteLine($"{item.Country}, {item.State}, {item.City}, {item.date}, {item.population}");
}

结果

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

您可以将两个列表连接起来,然后按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);
        }

相关内容

  • 没有找到相关文章