Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用groupby基于两列区分记录列表_C#_Entity Framework_List_Group By_Distinct - Fatal编程技术网

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

C# 使用groupby基于两列区分记录列表,c#,entity-framework,list,group-by,distinct,C#,Entity Framework,List,Group By,Distinct,我正在尝试使用EF6获取列表。我有一个类似这样的类: public class Province { public string province { set; get; } public string provinceCode { set; get; } } 区域级 namespace InnoviceDomainClass { using System; using System.Collections.Generic;

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

public class Province
    {
        public string province { set; get; }
        public string provinceCode { set; get; }
    }
区域级

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; }
    }
}
我想我的问题是
Distinct()
,我应该告诉这个函数基于哪个列应该是Distinct的? 但我的记录没有改变;为什么?我的记录是一样的

我的唱片是这样的

provincecode       province
10                 Iran
10                 Iran
15                 USA
15                 USA
我需要的输出:

provincecode       province
10                 Iran
15                 USA

您必须创建包含相等成员的部分类区域:

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实现:

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();
    }
}
公共类区域比较者:IEqualityComparer
{
公共布尔等于(x区、y区)
{
if(ReferenceEquals(x,y))返回true;
if(ReferenceEquals(x,null)| | ReferenceEquals(y,null))
返回false;
返回x等于(y);
}
public int GetHashCode(区域产品)
{
if(Object.ReferenceEquals(product,null))返回0;
return product.GetHashCode();
}
}
之后,您将能够执行这样的查询:

        List<Province> ListZoneWithDistinct = ListZone.Distinct(new ZoneComparer()).Select(i => new Province()
        {
            province = i.Province,
            provinceCode = i.ProvinceCode.Value.ToString()
        }).ToList();
List ListZoneWithDistinct=ListZone.Distinct(新建区域比较程序())。选择(i=>new Province())
{
省,省,
provinceCode=i.provinceCode.Value.ToString()
}).ToList();
编辑:

是的,区分是您的问题,要区分Lambda try():

列表区域不相同=
ZoneList.GroupBy(x=>new{x.Province,x.ProvinceCode})
.选择(grp=>新省()
{
省=grp.First().省,
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();
List ListZoneWithDistinct=
(来自ListZone中的lz)
选择新省()
{
省=lz.省,
provinceCode=lz.provinceCode.Value.ToString()
}).Distinct().ToList();

谢谢你,Paul,但是你的第二个方法不起作用,第一个方法有一个errorfirst方法错误:无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”@EA检查第一个方法-我已经更改了它。。。至于第二个问题,错误/问题是什么我还将其更改为。第一个错误再次出现:错误5无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”@EA我已经更改并测试了第一个,应该可以了。
        List<Province> ListZoneWithDistinct = ListZone.Distinct(new ZoneComparer()).Select(i => new Province()
        {
            province = i.Province,
            provinceCode = i.ProvinceCode.Value.ToString()
        }).ToList();
List<Province> ListZoneWithDistinct =
               ZoneList.GroupBy(x => new {x.Province, x.ProvinceCode})
               .Select(grp => new Province()
                {
                    province = grp.First().Province,
                    provinceCode = grp.First().ProvinceCode
                }).ToList();
List<Province> ListZoneWithDistinct = 
                                    (from lz in ListZone
                                     select new Province()
                                     {
                                         province = lz.Province,
                                         provinceCode = lz.ProvinceCode.Value.ToString()
                                     }).Distinct().ToList();