1

I am trying to fetch a list using EF6 .I have a class like this:

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

I fetch my data using this :

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();

I think my problem is Distinct() ,i should tell this function based on which column should be distinct? But my records don't change ;why ?and my records are same

my records is like this

provincecode       province
10                 Iran
10                 Iran
15                 USA
15                 USA

Output that i need:

provincecode       province
10                 Iran
15                 USA
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

2 Answers2

1

EDITED:

Yes Distinct is your problem, to Distinct a Lambda try (See it working here):

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

Or you could try the following LINQ / L2E:

List<Province> ListZoneWithDistinct = 
                                    (from lz in ListZone
                                     select new Province()
                                     {
                                         province = lz.Province,
                                         provinceCode = lz.ProvinceCode.Value.ToString()
                                     }).Distinct().ToList();
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • Thank you Paul but your second method doesn't work and the first one has an error – Ehsan Akbar Aug 15 '14 at 09:56
  • first method error :Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' – Ehsan Akbar Aug 15 '14 at 10:00
  • @EA Check the first method - I've altered it... and as for the second, what is the error/issue? - I've also altered that a little to. – Paul Zahra Aug 15 '14 at 10:08
  • The first one again have an error : Error 5 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' – Ehsan Akbar Aug 15 '14 at 10:14
  • @EA I've altered and tested the first one now, it should be fine for you. – Paul Zahra Aug 15 '14 at 10:55
0

You will have to create a partial class Zone with equality members:

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

Then you will need an IEqualityComparer implementation for 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();
    }
}

And after that you'll be able to execute such query:

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