问题
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
回答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();
回答2:
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();
来源:https://stackoverflow.com/questions/25324066/distinct-a-list-of-records-based-on-two-column-using-groupby