using System;
using System.Collections.Generic;
using System.Linq;
class Sample
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public override string ToString() {
return Value1 + "/" + Value2;
}
}
class SampleComparer : IEqualityComparer<Sample>
{
public bool Equals(Sample x, Sample y)
{
return x.Value2 == y.Value2;
}
public int GetHashCode(Sample obj)
{
return obj.Value2.GetHashCode();
}
}
public class Test
{
public static void Main()
{
var list = new List<Sample>() {
new Sample{ Value1 = "Apple", Value2="Red" },
new Sample{ Value1 = "Banana", Value2="Yellow" },
new Sample{ Value1 = "Lemon", Value2="Yellow" },
};
list = list.Distinct(new SampleComparer()).ToList();
foreach (var n in list) {
Console.WriteLine(n);
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CgpjbGFzcyBTYW1wbGUKewogICAgcHVibGljIHN0cmluZyBWYWx1ZTEgeyBnZXQ7IHNldDsgfQogICAgcHVibGljIHN0cmluZyBWYWx1ZTIgeyBnZXQ7IHNldDsgfQogICAgcHVibGljIG92ZXJyaWRlIHN0cmluZyBUb1N0cmluZygpIHsKICAgICAgICByZXR1cm4gVmFsdWUxICsgIi8iICsgVmFsdWUyOwogICAgfQp9CgpjbGFzcyBTYW1wbGVDb21wYXJlciA6IElFcXVhbGl0eUNvbXBhcmVyPFNhbXBsZT4KewogICAgcHVibGljIGJvb2wgRXF1YWxzKFNhbXBsZSB4LCBTYW1wbGUgeSkKICAgIHsKICAgICAgICByZXR1cm4geC5WYWx1ZTIgPT0geS5WYWx1ZTI7CiAgICB9CiAgICBwdWJsaWMgaW50IEdldEhhc2hDb2RlKFNhbXBsZSBvYmopCiAgICB7CiAgICAgICAgcmV0dXJuIG9iai5WYWx1ZTIuR2V0SGFzaENvZGUoKTsKICAgIH0KfQoKcHVibGljIGNsYXNzIFRlc3QKewoJcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQoJewoJCXZhciBsaXN0ID0g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