自定义集合去重复
class Program
{
static void Main(string[] args)
{
var list = new List<TempData>
{
new TempData{ name="a", tel="010", address="add"},
new TempData{ name="a", tel="010", address="add"},
new TempData{ name="a1", tel="010", address="add"},
new TempData{ name="a1", tel="010", address="add"},
new TempData{ name="a2", tel="010", address="add"}
};
Console.WriteLine("name\ttel\taddress");
list.Distinct(new DataRowComparer()).ToList().ForEach(l => Console.WriteLine("{0}\t{1}\t{2}", l.name, l.tel, l.address));
//list.ForEach(l => Console.WriteLine("{0}\t{1}\t{2}", l.name, l.tel, l.address));
}
public class TempData
{
public string name { get; set; }
public string tel { get; set; }
public string address { get; set; }
}
public class DataRowComparer : IEqualityComparer<TempData>
{
public bool Equals(TempData t1, TempData t2)
{
return (t1.name == t2.name && t1.tel == t2.tel && t1.address == t2.address);
}
public int GetHashCode(TempData t)
{
return t.ToString().GetHashCode();
}
}
}
{
static void Main(string[] args)
{
var list = new List<TempData>
{
new TempData{ name="a", tel="010", address="add"},
new TempData{ name="a", tel="010", address="add"},
new TempData{ name="a1", tel="010", address="add"},
new TempData{ name="a1", tel="010", address="add"},
new TempData{ name="a2", tel="010", address="add"}
};
Console.WriteLine("name\ttel\taddress");
list.Distinct(new DataRowComparer()).ToList().ForEach(l => Console.WriteLine("{0}\t{1}\t{2}", l.name, l.tel, l.address));
//list.ForEach(l => Console.WriteLine("{0}\t{1}\t{2}", l.name, l.tel, l.address));
}
public class TempData
{
public string name { get; set; }
public string tel { get; set; }
public string address { get; set; }
}
public class DataRowComparer : IEqualityComparer<TempData>
{
public bool Equals(TempData t1, TempData t2)
{
return (t1.name == t2.name && t1.tel == t2.tel && t1.address == t2.address);
}
public int GetHashCode(TempData t)
{
return t.ToString().GetHashCode();
}
}
}