You would need to define the Equals
and GetHashCode
of CurrencyDetail
to do what you want. Quick and dirty solution:
var data = (from l in lines.Skip(1)
let split = l.Split(',')
select new
{
ISOName = split[3],
ISOCode = split[3]
}).Distinct()
.Select(x => new CurrencyDetail
{
ISOName = x.ISOName,
ISOCode = x.ISOCode
};
Anonymous types (the first new { ... }
) automatically define sensible Equals()
and GetHashCode()
. Normally I wouldn't do this, because you are creating objects to then discard them. For this reason it is a quick and dirty solution.
Note that you are using twice split[3]
... an error?
Now, a fully equatable version of CurrencyDetail
could be:
public class CurrencyDetail : IEquatable<CurrencyDetail>
{
public string ISOName { get; set; }
public string ISOCode { get; set; }
public override bool Equals(object obj)
{
// obj is object, so we can use its == operator
if (obj == null)
{
return false;
}
CurrencyDetail other = obj as CurrencyDetail;
if (object.ReferenceEquals(other, null))
{
return false;
}
return this.InnerEquals(other);
}
public bool Equals(CurrencyDetail other)
{
if (object.ReferenceEquals(other, null))
{
return false;
}
return this.InnerEquals(other);
}
private bool InnerEquals(CurrencyDetail other)
{
// Here we know that other != null;
if (object.ReferenceEquals(this, other))
{
return true;
}
return this.ISOName == other.ISOName && this.ISOCode == other.ISOCode;
}
public override int GetHashCode()
{
unchecked
{
// From http://stackoverflow.com/a/263416/613130
int hash = 17;
hash = hash * 23 + (this.ISOName != null ? this.ISOName.GetHashCode() : 0);
hash = hash * 23 + (this.ISOCode != null ? this.ISOCode.GetHashCode() : 0);
return hash;
}
}
}
With this you can use the Distinct()
as used by your code.