public void RemoveDuplicatesByName()

{
List<Posting> postings = GetAllPostings();
postings.Sort();
for (int i = 1; i < postings.Count; i++)
{
if (postings[i].Name == postings[i - 1].Name)
{
postings.RemoveAt(i);
i--;
}
}
}
public List<Posting> GetAllPostings()

{
List<Posting> postings = new List<Posting>();
Posting posting;
posting = new Posting("post1", "2005-5-11");
postings.Add(posting);
posting = new Posting("post1", "2005-5-11");
postings.Add(posting);
posting = new Posting("post2", "2005-6-11");
postings.Add(posting);
posting = new Posting("post2", "2005-5-11");
postings.Add(posting);
posting = new Posting("post3", "2005-8-11");
postings.Add(posting);
posting = new Posting("post2", "2005-7-11");
postings.Add(posting);
posting = new Posting("post1", "2005-5-12");
postings.Add(posting);
posting = new Posting("post2", "2005-8-11");
postings.Add(posting);
return postings;
}
public class Posting : IComparable<Posting>

{
private string name;
private string lastModifiedDate;
public Posting(string name, string lastModifiedDate)
{
this.name = name;
this.lastModifiedDate = lastModifiedDate;
}
public string Name
{
get
{ return name; }
set
{ name = value; }
}
public string LastModifiedDate
{
get
{ return lastModifiedDate; }
set
{ lastModifiedDate = value; }
}
public int CompareTo(Posting other)
{
return (String.Compare(this.Name, other.Name));
}
}
本文介绍了一种通过名称属性来去除列表中重复项的方法。首先获取所有博客条目,然后按名称排序,接着遍历列表并移除重复的条目。此过程确保每个名称只保留一个实例。
1851

被折叠的 条评论
为什么被折叠?



