简体   繁体   中英

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

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:

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

Note that this is using C#7 tuple syntax. 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));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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