如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便)
生活随笔
收集整理的這篇文章主要介紹了
如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.net 1.1中的解決方法(轉)
1建立一個DataSetHelper類(DataSetHelper.cs) public?class?DataSetHelper
...{
????public?DataSet?ds;
????public?DataSetHelper(ref?DataSet?DataSet)
????...{
????????ds?=?DataSet;
????}
????public?DataSetHelper()
????...{
????????ds?=?null;
????}
????private?bool?ColumnEqual(object?A,?object?B)
????...{
????????if?(A?==?DBNull.Value?&&?B?==?DBNull.Value)?//??both?are?DBNull.Value
????????????return?true;
????????if?(A?==?DBNull.Value?||?B?==?DBNull.Value)?//??only?one?is?DBNull.Value
????????????return?false;
????????return?(A.Equals(B));??//?value?type?standard?comparison
????}
????public?DataTable?SelectDistinct(string?TableName,?DataTable?SourceTable,?string?FieldName)
????...{
????????DataTable?dt?=?new?DataTable(TableName);
????????dt.Columns.Add(FieldName,?SourceTable.Columns[FieldName].DataType);
????????object?LastValue?=?null;
????????foreach?(DataRow?dr?in?SourceTable.Select("",?FieldName))
????????...{
????????????if?(LastValue?==?null?||?!(ColumnEqual(LastValue,?dr[FieldName])))
????????????...{
????????????????LastValue?=?dr[FieldName];
????????????????dt.Rows.Add(new?object[]?...{?LastValue?});
????????????}
????????}
????????if?(ds?!=?null)
????????????ds.Tables.Add(dt);
????????return?dt;
????}
} 2 建立一個Web窗體,在page_load中寫下面的代碼 ???????DataSet?ds;
????????DataSetHelper?dsHelper;
????????ds?=?new?DataSet();
????????dsHelper?=?new?DataSetHelper(ref?ds);
????????//?Create?source?table
????????DataTable?dt?=?new?DataTable("Orders");
????????dt.Columns.Add("EmployeeID",?Type.GetType("System.String"));
????????dt.Columns.Add("OrderID",?Type.GetType("System.Int32"));
????????dt.Columns.Add("Amount",?Type.GetType("System.Decimal"));
????????dt.Rows.Add(new?object[]?...{?"Sam",?5,?25.00?});
????????dt.Rows.Add(new?object[]?...{?"Tom",?7,?50.00?});
????????dt.Rows.Add(new?object[]?...{?"Sue",?9,?11.00?});
????????dt.Rows.Add(new?Object[]?...{?"Tom",?12,?7.00?});
????????dt.Rows.Add(new?Object[]?...{?"Sam",?14,?512.00?});
????????dt.Rows.Add(new?Object[]?...{?"Sue",?15,?17.00?});
????????dt.Rows.Add(new?Object[]?...{?"Sue",?22,?2.50?});
????????dt.Rows.Add(new?object[]?...{?"Tom",?24,?3.00?});
????????dt.Rows.Add(new?object[]?...{?"Tom",?33,?78.75?});
????????ds.Tables.Add(dt);
???????DataTable?td=dsHelper.SelectDistinct("DistinctEmployees",?ds.Tables["Orders"],?"EmployeeID");
???????this.GridView1.DataSource?=?td;
???????this.GridView1.DataBind();
????????{
????????????DataSet?dsKeyword?=?dal.GetKeyword();
????????????DataSet?dsTopSearch?=?new?DataSet();
????????????for?(int?i?=?0;?i?<?4;?i++)
????????????{
????????????????string?keyword?=?dsKeyword.Tables[0].Rows[i]["Name"].ToString();
????????????????string?condition?=?dsKeyword.Tables[0].Rows[i]["SearchCondition"].ToString();
????????????????dsTopSearch.Merge(dal.GetTopSearch(keyword,condition));
????????????}
????????????return?dsTopSearch.Tables[0].DefaultView.ToTable(true,?"ID","Name","Author","Publisher","PublishDate","TypeName","Price","SalePrice","SavePrice","Rebate","ImagePath","ContentIntro");
????????}
1建立一個DataSetHelper類(DataSetHelper.cs) public?class?DataSetHelper
...{
????public?DataSet?ds;
????public?DataSetHelper(ref?DataSet?DataSet)
????...{
????????ds?=?DataSet;
????}
????public?DataSetHelper()
????...{
????????ds?=?null;
????}
????private?bool?ColumnEqual(object?A,?object?B)
????...{
????????if?(A?==?DBNull.Value?&&?B?==?DBNull.Value)?//??both?are?DBNull.Value
????????????return?true;
????????if?(A?==?DBNull.Value?||?B?==?DBNull.Value)?//??only?one?is?DBNull.Value
????????????return?false;
????????return?(A.Equals(B));??//?value?type?standard?comparison
????}
????public?DataTable?SelectDistinct(string?TableName,?DataTable?SourceTable,?string?FieldName)
????...{
????????DataTable?dt?=?new?DataTable(TableName);
????????dt.Columns.Add(FieldName,?SourceTable.Columns[FieldName].DataType);
????????object?LastValue?=?null;
????????foreach?(DataRow?dr?in?SourceTable.Select("",?FieldName))
????????...{
????????????if?(LastValue?==?null?||?!(ColumnEqual(LastValue,?dr[FieldName])))
????????????...{
????????????????LastValue?=?dr[FieldName];
????????????????dt.Rows.Add(new?object[]?...{?LastValue?});
????????????}
????????}
????????if?(ds?!=?null)
????????????ds.Tables.Add(dt);
????????return?dt;
????}
} 2 建立一個Web窗體,在page_load中寫下面的代碼 ???????DataSet?ds;
????????DataSetHelper?dsHelper;
????????ds?=?new?DataSet();
????????dsHelper?=?new?DataSetHelper(ref?ds);
????????//?Create?source?table
????????DataTable?dt?=?new?DataTable("Orders");
????????dt.Columns.Add("EmployeeID",?Type.GetType("System.String"));
????????dt.Columns.Add("OrderID",?Type.GetType("System.Int32"));
????????dt.Columns.Add("Amount",?Type.GetType("System.Decimal"));
????????dt.Rows.Add(new?object[]?...{?"Sam",?5,?25.00?});
????????dt.Rows.Add(new?object[]?...{?"Tom",?7,?50.00?});
????????dt.Rows.Add(new?object[]?...{?"Sue",?9,?11.00?});
????????dt.Rows.Add(new?Object[]?...{?"Tom",?12,?7.00?});
????????dt.Rows.Add(new?Object[]?...{?"Sam",?14,?512.00?});
????????dt.Rows.Add(new?Object[]?...{?"Sue",?15,?17.00?});
????????dt.Rows.Add(new?Object[]?...{?"Sue",?22,?2.50?});
????????dt.Rows.Add(new?object[]?...{?"Tom",?24,?3.00?});
????????dt.Rows.Add(new?object[]?...{?"Tom",?33,?78.75?});
????????ds.Tables.Add(dt);
???????DataTable?td=dsHelper.SelectDistinct("DistinctEmployees",?ds.Tables["Orders"],?"EmployeeID");
???????this.GridView1.DataSource?=?td;
???????this.GridView1.DataBind();
?.net 2.0中的解決方法(原創)
????????{
????????????DataSet?dsKeyword?=?dal.GetKeyword();
????????????DataSet?dsTopSearch?=?new?DataSet();
????????????for?(int?i?=?0;?i?<?4;?i++)
????????????{
????????????????string?keyword?=?dsKeyword.Tables[0].Rows[i]["Name"].ToString();
????????????????string?condition?=?dsKeyword.Tables[0].Rows[i]["SearchCondition"].ToString();
????????????????dsTopSearch.Merge(dal.GetTopSearch(keyword,condition));
????????????}
????????????return?dsTopSearch.Tables[0].DefaultView.ToTable(true,?"ID","Name","Author","Publisher","PublishDate","TypeName","Price","SalePrice","SavePrice","Rebate","ImagePath","ContentIntro");
????????}
先把DataTable轉成DataView,再通過DataView.ToTable()轉回DataTable,ToTable()方法中有一個重載可以輕松消除重復行.
注:該重載的第二個參數為要保存的字段名.
總結
以上是生活随笔為你收集整理的如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: reactnative 获取定位_[RN
- 下一篇: system函数_自学C++基础教程【函