如何去掉DataTable中的重复行
public DataTable GetTopSearch() { 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()方法中有一个重载可以轻松消除重复行.
注:该重载的第二个参数为要保存的字段名.