读书人

怎么获取DataTable表中去除所有重复

发布时间: 2013-11-29 13:49:33 作者: rapoo

【求助】如何获取DataTable表中,去除所有重复项之后的数据,注意:重复的数据不要保留第一个,直接都删除掉
具体实例来说吧:


DataTable SourceDT = new DataTable();
SourceDt.columns.Add("Id",System.Type.GetType("System.String"));
SourceDt.columns.Add("Name",System.Type.GetType("System.String"));
SourceDt.columns.Add("Age",System.Type.GetType("System.Int32"));
SourceDt.columns.Add("Sex",System.Type.GetType("System.String"));

sourceDT.Rows.Add(new object[] { "10001", "李一", 24, "男" });
sourceDT.Rows.Add(new object[] { "10001", "王二", 23, "男" });
sourceDT.Rows.Add(new object[] { "10001", "孙三", 24, "女" });
sourceDT.Rows.Add(new object[] { "10002", "李四", 23, "男" });
sourceDT.Rows.Add(new object[] { "10002", "王五", 24, "女" });
sourceDT.Rows.Add(new object[] { "10003", "冯六", 25, "男" });
sourceDT.Rows.Add(new object[] { "10003", "陈七", 22, "女" });
sourceDT.Rows.Add(new object[] { "10003", "刘八", 20, "女" });
sourceDT.Rows.Add(new object[] { "10004", "周九", 26, "男" });
sourceDT.Rows.Add(new object[] { "10005", "周十", 21, "女" });
sourceDT.Rows.Add(new object[] { "10006", "孙A", 22, "女" });
sourceDT.Rows.Add(new object[] { "10007", "邹B", 21, "女" });
sourceDT.Rows.Add(new object[] { "10008", "王C", 25, "男" });


结构:SourceDT
Id Name Age Sex
10001 李一 24 男
10001 王二 23 男
10001 孙三 24 女
10002 李四 23 男
10002 王五 24 女
10003 冯六 25 男
10003 陈七 22 女
10003 刘八 20 女
10004 周九 26 男
10005 周十 21 女
10006 孙A 22 女
10007 邹B 21 女
10008 王C 25 男

我想要的效果: 去除所有的重复项,重复的那一项不要保留第一个,直接全部删除,如下图示:
去除重复项后结构:
Id Name Age Sex


10004 周九 26 男
10005 周十 21 女
10006 孙A 22 女
10007 邹B 21 女
10008 王C 25 男


请问如何实现啊???
DataTable DataRow
[解决办法]

引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestEDM;
using TestBLL;
using System.IO;
using System.Data;

namespace TestWebClient
{
public partial class Test : System.Web.UI.Page
{
List<Student> listStudent = new List<Student>();
protected void Page_Load(object sender, EventArgs e)
{
DistinctStudent();

}

private void DistinctStudent()
{
listStudent.Add(new Student { ID = "10001", Name = "李一", Age = 24, Sex = "男" });
listStudent.Add(new Student { ID = "10001", Name = "王二", Age = 23, Sex = "男" });
listStudent.Add(new Student { ID = "10001", Name = "孙三", Age = 24, Sex = "女" });
listStudent.Add(new Student { ID = "10002", Name = "李四", Age = 23, Sex = "男" });
listStudent.Add(new Student { ID = "10002", Name = "王五", Age = 24, Sex = "女" });
listStudent.Add(new Student { ID = "10003", Name = "冯六", Age = 25, Sex = "男" });
listStudent.Add(new Student { ID = "10003", Name = "陈七", Age = 22, Sex = "女" });
listStudent.Add(new Student { ID = "10003", Name = "刘八", Age = 20, Sex = "女" });
listStudent.Add(new Student { ID = "10004", Name = "周九", Age = 26, Sex = "男" });
listStudent.Add(new Student { ID = "10005", Name = "周十", Age = 21, Sex = "女" });
listStudent.Add(new Student { ID = "10006", Name = "孙A", Age = 22, Sex = "女" });


listStudent.Add(new Student { ID = "10007", Name = "邹B", Age = 21, Sex = "女" });
listStudent.Add(new Student { ID = "10008", Name = "王C", Age = 25, Sex = "男" });

var alist = listStudent.GroupBy(g => g.ID).Select(t => { return new { ID = t.Key, count = t.Count() }; });
var list = from a in listStudent join b in alist on a.ID equals b.ID where b.count == 1 select a;
foreach (var item in list)
{
Response.Write(item.ID + "---" + item.Name + "---" + item.Age + "---" + item.Sex + "</br>");
}
}
}
public class Student
{
static Student()
{ }
public string ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}




打印结果:
10004---周九---26---男
10005---周十---21---女
10006---孙A---22---女
10007---邹B---21---女
10008---王C---25---男
怎么获取DataTable表中,去除所有重复项之后的数据,注意:重复的数据不要保留第一个,直接都删除掉

读书人网 >C#

热点推荐