使用groupby区分基于两列的记录列表

本文关键字:记录 列表 于两列 groupby 使用 | 更新日期: 2023-09-27 18:18:03

我正在尝试使用EF6获取列表。我有一个这样的类:

public class Province
    {
        public string province { set; get; }
        public string provinceCode { set; get; }
    }

Zone class

namespace InnoviceDomainClass
{
    using System;
    using System.Collections.Generic;
    public partial class Zone
    {
        public string CityCode { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public Nullable<int> ProvinceCode { get; set; }
    }
}

我获取我的数据使用:

List<Zone> ListZone = zoneRepository.GetAll().ToList();

i need to Distinct my records:

   List<Province> ListZoneWithDistinct = ListZone.Select(i => new Province()
            {
                province = i.Province,
                provinceCode = i.ProvinceCode.Value.ToString()
            }).Distinct().ToList();

我想我的问题是Distinct(),我应该告诉这个函数基于哪一列应该是不同的?但是我的记录没有改变,为什么?而且我的记录是一样的

我的记录是这样的

provincecode       province
10                 Iran
10                 Iran
15                 USA
15                 USA

需要的输出:

provincecode       province
10                 Iran
15                 USA

使用groupby区分基于两列的记录列表

edit:

Yes Distinct是你的问题,要区分一个Lambda尝试(看看它在这里工作):

List<Province> ListZoneWithDistinct =
               ZoneList.GroupBy(x => new {x.Province, x.ProvinceCode})
               .Select(grp => new Province()
                {
                    province = grp.First().Province,
                    provinceCode = grp.First().ProvinceCode
                }).ToList();

或者您可以尝试下面的LINQ/L2E:

List<Province> ListZoneWithDistinct = 
                                    (from lz in ListZone
                                     select new Province()
                                     {
                                         province = lz.Province,
                                         provinceCode = lz.ProvinceCode.Value.ToString()
                                     }).Distinct().ToList();

您必须创建一个具有相等成员的分区类Zone:

public partial class Zone
{
    protected bool Equals(Zone other)
    {
        if (ReferenceEquals(other, null))
        {
            return false;
        }
        if (ReferenceEquals(other, this))
        {
            return true;
        }
        return string.Equals(CityCode, other.CityCode) &&
               string.Equals(City, other.City) &&
               string.Equals(Province, other.Province) &&
               ProvinceCode == other.ProvinceCode;
    }
    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = (CityCode != null ? CityCode.GetHashCode() : 0);
            hashCode = (hashCode*397) ^ (City != null ? City.GetHashCode() : 0);
            hashCode = (hashCode*397) ^ (Province != null ? Province.GetHashCode() : 0);
            hashCode = (hashCode*397) ^ ProvinceCode.GetHashCode();
            return hashCode;
        }
    }
    public static bool operator ==(Zone left, Zone right)
    {
        return Equals(left, right);
    }
    public static bool operator !=(Zone left, Zone right)
    {
        return !Equals(left, right);
    }
    public override bool Equals(object obj)
    {
        return Equals(obj as Zone);
    }
}

那么你将需要一个IEqualityComparer实现Zone:

public class ZoneComparer : IEqualityComparer<Zone>
{
    public bool Equals(Zone x, Zone y)
    {
        if (ReferenceEquals(x, y)) return true;
        if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            return false;
        return x.Equals(y);
    }
    public int GetHashCode(Zone product)
    {
        if (Object.ReferenceEquals(product, null)) return 0;
        return product.GetHashCode();
    }
}

之后,你将能够执行这样的查询:

        List<Province> ListZoneWithDistinct = ListZone.Distinct(new ZoneComparer()).Select(i => new Province()
        {
            province = i.Province,
            provinceCode = i.ProvinceCode.Value.ToString()
        }).ToList();