首页 > Asp.net开发 > 如何去除List集合中重复的元素

如何去除List集合中重复的元素

 // <summary>
/// 去除List集合中的重复元素
/// </summary>
/// <param name="list">要去除重复元素的集合</param>
/// <returns>返回处理后的集合</returns>
public static List<TypeAndName> ItemFilter(List<TypeAndName> list)
{
List<TypeAndName> resultList = new List<TypeAndName>();
for (int i = 0; i < list.Count; i++)
{
for (int j = i + 1; j < list.Count; j++)
{
if (list[i].Name == list[j].Name && list[i].Name != "重复值")
{
list[j].Name = "重复值";
}
}
}
for (int i = 0; i < list.Count; i++)
{
if (list[i].Name != "重复值")
{
TypeAndName single = new TypeAndName();
single.Name = list[i].Name;
single.Type = list[i].Type;
resultList.Add(single);
}
}

            return resultList;
}


本文固定链接: http://www.devba.com/index.php/archives/4347.html | 开发吧

报歉!评论已关闭.