数据集中的行重复

时间:2014-03-05 11:01:49

标签: c# dataset

enter image description here

我有一个问题我不知道如何做这个我有一个数据集,如果它有重复行然后我想打印一条消息。任何人都可以建议我怎么做这个。以下是代码

bool ErrorOcc = false;
StringBuilder script = new StringBuilder();
string Filter = "";
string XmlCheck = "";
string[] IfExists = null;
for (int i = 0; i < ColumnsToks[0].Length; i++)
{
    if (ColumnsToks[0][i] != "")
    {
        xml += @"<" + objModule.TableName + ">";
        for (int j = 0; j < objModule.Cols.Count; j++)
        {
            MCol col = objModule.Cols[j];
            DataTable dt = EBusiness.GetModuleColsValidation(this.Data.MID, col.ID);
            if (dt.Select("ValidationOnClient = 'False' and ValidationType = 'Unique'").Length == 0)
            {

            }
            for (int m = 0; m < dt.Rows.Count; m++)
            {
                 if (!Convert.ToBoolean(dt.Rows[m]["ValidationOnClient"]) && dt.Rows[m]["ValidationType"].ToString() == "Unique")
                 {
                     //  Filter += col.ColName + "='" + ColumnsToks[j][i] + "' And ";
                     XmlCheck += col.ColName + "='" + ColumnsToks[j][i] + "',";
                  }
             }
             IfExists = XmlCheck.Split(",".ToCharArray());
             xml += "<" + col.ColName + ">" + ColumnsToks[j][i].Replace("<", "&lt;").Replace(">", "&gt;").Replace("&nbsp;", "") + "</" + col.ColName + ">";
         }
         bool IfDuplicate = false;
         foreach (string count in IfExists)
         {
             if (count != "")
             {
                 Filter += count + " and ";
             }
         }
         objData.Query = "select COUNT(*) from " + objModule.TableName + " where " + Filter + " 1=1 ";
         IfDuplicate = Convert.ToBoolean(objData.GetSingleValue());
         if (IfDuplicate)
         {
             script.Append("alert('Error - There are some Duplicate entries.'); ");
             ErrorOcc = true;
         }
         if (ErrorOcc)
         {
             this.ScriptOutput = script + " ValidateBeforeSaving = false;";
             this.StayContent = "yes";
             return;
         }
         if (DataSetupColumnName != "")
         {
             foreach (MModule module in dataSetupModules.Modules)
             {
                 xml += "<" + module.TableName + ">" + Request["d" + module.TableName] + "</" + module.TableName + ">";
             }
          }
          foreach (DataRow dr in dtChildModule.Rows)
          {
              if (Request["t" + dr["ParentModuleID"]] == "")
              {
                  this.Message = "Please select Parent Module ID.";
                  return;
              }
              xml += "<t" + dr["ParentModuleID"] + ">" + Convert.ToInt32("0" + Request["t" + dr["ParentModuleID"]]) + "</t" + dr["ParentModuleID"] + ">";
          }
          xml += "<Status>1</Status><CreatedBy>0</CreatedBy><UpdatedBy>0</UpdatedBy>";
          if (dtRestrictions.Rows.Count > 0)
          {
               xml += "<Level>" + GetLevel(EBusiness.GetCompanySetupModules(this.Data.WFID)) + "</Level>";
               xml += "<LevelDataID>" + Request["t" + Request["ParentModuleID"]] + "</LevelDataID>";
          }
          xml += "</" + objModule.TableName + ">";
      }
  }
  xml += "</Table>";
  DataSet dsXml = new DataSet();
  dsXml.ReadXml(new XmlTextReader(new StringReader(xml)));

2 个答案:

答案 0 :(得分:1)

有几种方法可以使它工作,我想到的前两种方法是使用HashTables或LinQ表达式。

看看这个: Best way to remove duplicate entries from a data table 但不是删除副本(查看第二个foreach)而是打印消息。

public void CheckDuplicateRows(DataTable dTable, string colName)
{
   Hashtable hTable = new Hashtable();
   ArrayList duplicateList = new ArrayList();

   //Add list of all the unique item value to hashtable, which stores combination of     key, value pair.
   //And add duplicate item value in arraylist.
   foreach (DataRow drow in dTable.Rows)
   {
      if (hTable.Contains(drow[colName]))
         duplicateList.Add(drow);
      else
         hTable.Add(drow[colName], string.Empty); 
   }

  //Checks the list dimension to verify if there is any duplicate
  if(duplicateList.Count() > 0)
  {
  //you can print your message here or eventually get info about the duplicate row
  }   
}

答案 1 :(得分:0)

以上是我上述问题的答案

您可以检查DataSet中的重复行,这样可以正常工作,请尝试:

DataSet dsXml = new DataSet();
dsXml.ReadXml(new XmlTextReader(new StringReader(xml)));

List<string> duplicateList = new List<string>();
foreach (DataRow drow in dsXml.Tables[0].Rows)
{
    string strr = "";

    for (int j = 0; j < dsXml.Tables[0].Columns.Count; j++ )
    {
        strr += drow[j];
    }

    if (!duplicateList.Contains(strr))
    {
        duplicateList.Add(strr);
    }
    else
    {
        script.Append("alert('Error - There are some Duplicate entries.'); ");
        ErrorOcc = true;
        if (ErrorOcc)
        {
            this.ScriptOutput = script + " ValidateBeforeSaving = false;";
            this.StayContent = "yes";
            return;
        }
    }
}