|
Fengzhimei@Dot.Net
Designing My Colorful Dream |
CREATE TABLE [dbo].[postings] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[postingname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[postedby] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
)
DELETE FROM postings
WHERE EXISTS
(
SELECT NULL FROM postings b
WHERE
b.[postingname] = postings.[postingname] AND
b.[postedby] = postings.[postedby]
GROUP BY
b.[postingname], b.[postedby]
HAVING
postings.[id] < MAX(b.[id])
)
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));
}
}