简体   繁体   English

从列表C#中获取组合的不同项

[英]Get distinct items by combination from list C#

My object like like this 我的对象就像这样

    public class Region
{
    public Region();

    public string City { get; set; }
    public int PostCode { get; set; }
    public string WfRegion { get; set; }
}

I have a list of this objects in this class Where data is like this 我在这个类中有这个对象的列表,其中数据是这样的

Rodney , 7845 , Auckland
Rodney , 3435 , Auckland
Rodney , 4566 , Auckland
Rodney , 3445 , North Island

I want to filter this list so that I can get an output like this 我想过滤这个列表,这样我就可以获得这样的输出

    Rodney , 7845 , Auckland
    Rodney , 3445 , North Island

(all the possible combination of city and region regardless of postcode). (所有可能的城市和地区组合,无论邮政编码)。 I have wrote some query like this 我写了一些像这样的查询

      var cities = regionsData.DistinctBy(p => 
p.WfRegion).DistinctBy(p=>p.PostCode).DistinctBy(p => p.City).ToList();

But this is giving me a result of first item only like this 但这只是给我一个第一项的结果

        Rodney , 7845 , Auckland

How can I solve this issue? 我该如何解决这个问题?

You need to use GroupBy 您需要使用GroupBy

var result = regionsData.GroupBy(p => new {p.WfRegion, p.City})
    .Select(g => g.First())
    .ToList();

That will give you groupings by the region and city and then you can just select the first item in each group. 这将为您提供区域和城市的分组,然后您可以选择每个组中的第一个项目。

You can use DistinctBy to solve this as follows: 您可以使用DistinctBy解决此问题,如下所示:

var cities = regionsData.DistinctBy(x => (x.City, x.WfRegion));

Note that this is using C#7 tuple syntax. 请注意,这是使用C#7元组语法。 For older versions you must use an anonymous type as follows: 对于旧版本,您必须使用匿名类型,如下所示:

var cities = regionsData.DistinctBy(x => new {x.City, x.WfRegion});

Full console example: 完整控制台示例:

using System;
using System.Collections.Generic;
using MoreLinq;

namespace ConsoleApp1
{
    public class Region
    {
        public string City     { get; set; }
        public int    PostCode { get; set; }
        public string WfRegion { get; set; }

        public override string ToString()
        {
            return  $"City:{City}, PostCode:{PostCode}, WfRegion:{WfRegion}";
        }
    }

    class Program
    {
        static void Main()
        {
            IEnumerable<Region> regions = new []
            {
                new Region { City = "CityOne", PostCode = 1, WfRegion = "WfRegionOne"},
                new Region { City = "CityOne", PostCode = 2, WfRegion = "WfRegionTwo"},
                new Region { City = "CityTwo", PostCode = 3, WfRegion = "WfRegionOne"},
                new Region { City = "CityOne", PostCode = 4, WfRegion = "WfRegionOne"},
                new Region { City = "CityOne", PostCode = 5, WfRegion = "WfRegionThree"},
                new Region { City = "CityTwo", PostCode = 6, WfRegion = "WfRegionOne"},
                new Region { City = "CityTwo", PostCode = 7, WfRegion = "WfRegionThree"}
            };

            var result = regions.DistinctBy(x => (x.City, x.WfRegion));

            Console.WriteLine(string.Join("\n", result));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM