C#去除List中集合的重复项(类型对象和单一类型)

去除重复类型对象BookInfo示例:

bookList = bookList.Distinct(new DataRowComparer()).ToList(); //去除重复书籍

/// <summary>
/// 自定义书籍比较(去重)
/// </summary>
public class DataRowComparer : IEqualityComparer<BookInfo>
{
    public bool Equals(BookInfo b1, BookInfo b2)
    {
        return (b1.BookId == b2.BookId); //去重
    }
    public int GetHashCode(BookInfo b)
    {
        return b.ToString().GetHashCode();
    }
}

 

去除单一类型元素:

List<string> list = new List<string>();
list.Add("a");
list.Add("a");
list.Add("b");
list.Add("c");
list.Add("b");
list = list.Distinct().ToList();

你可能感兴趣的:(list)