Concatenate data but ignore duplicate lines

I have the following code, its a custom picker for sharepoint 2010.

It searches by username, but also by person's name. Since it contains a search, if I try with part of my username:cia

It shows my duplicate lines as it matches the username as well as the person's name.

this is my code (i cannot use LINQ:

 protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize)
 {
    try
    {
        // Find any user that has a matching name
        var table = ADHelper.ExecuteNameQuery(RootPath, search);

        // 20249: Search by username, method was already done, but it was not being called.
        var table2 = ADHelper.ExecutesAMAccountNameQuery(search);
        table2.Merge(table,);
        PickerDialog.Results = table2;

      

+3


source to share


2 answers


Typically, the method DataTable.Merge

removes duplicates implicitly. But only when the values โ€‹โ€‹of all columns are the same.

I'm not sure if there is something simpler (you mentioned you cannot use LINQ), but you can combine both and remove duplicates:

List<string> dupColumns = new List<string>();
dupColumns.Add("ColumnA");
dupColumns.Add("ColumnB");
table2.Merge(table,);
RemoveDuplicates(table2, dupColumns);

      



And here's the remove-duplicates function:

private void RemoveDuplicates(DataTable table, List<string> keyColumns)
{
    Dictionary<string, string> uniquenessDict = new Dictionary<string, string>(table.Rows.Count);
    System.Text.StringBuilder sb = null;
    int rowIndex = 0;
    DataRow row;
    DataRowCollection rows = table.Rows;
    while (rowIndex < rows.Count)  
    {
        row = rows[rowIndex];
        sb = new System.Text.StringBuilder();
        foreach (string colname in keyColumns)
        {
            sb.Append(((string)row[colname]));
        }

        if (uniquenessDict.ContainsKey(sb.ToString()))
        {
            rows.Remove(row);
        }
        else
        {
            uniquenessDict.Add(sb.ToString(), string.Empty);
            rowIndex++;
        }
    }
}

      

+7


source


you should the .ToTable function 

      

here is a sample code



        DataTable DT1 = new DataTable();
    DT1.Columns.Add("c_" + DT1.Columns.Count);
    DT1.Columns.Add("c_" + DT1.Columns.Count);
    DT1.Columns.Add("c_" + DT1.Columns.Count);

    DataRow DR = DT1.NewRow();
    DR[0] = 0;
    DR[1] = 1;
    DR[2] = 2;
    DT1.Rows.Add(DR);

    DataTable DT2 = new DataTable();
    DT2.Columns.Add("c_" + DT2.Columns.Count);
    DT2.Columns.Add("c_" + DT2.Columns.Count);
    DT2.Columns.Add("c_" + DT2.Columns.Count);
    DT2.Columns.Add("c_" + DT2.Columns.Count);

    DR = DT2.NewRow();
    DR[0] = 0;
    DR[1] = 1;
    DR[2] = 2;
    DR[3] = 3;
    DT2.Rows.Add(DR);

    DT1.Merge(DT2);
    Trace.IsEnabled = true;
    DataTable DT_3=DT1.DefaultView.ToTable(true,new string[]{"c_1","c_2","c_0"});
    foreach (DataRow CDR in DT_3.Rows)
    {
        Trace.Warn("val",CDR[1]+"");//you will find only one data row
    }

      

+1


source







All Articles