2

The following C# code takes a large datatable with many columns and an array of 2 column names. It will give a new datatable with two rows where there are duplicate rows for the two fields supplied staff no & skill. This is too specific and I need to supply any number of fields as the groupby. can someone help me?

string[] excelField = new string[0]; // contains a list of field name for uniquness
excelField[0] = "staff No";
excelField[1] = "skill";
DataTable dataTableDuplicateRows = new DataTable();
dataTableDuplicateRows.Clear();
dataTableDuplicateRows.Columns.Clear();

foreach (string fieldName in excelField)
{
    dataTableDuplicateRows.Columns.Add(fieldName);
}

var duplicateValues = dataTableCheck.AsEnumerable()
    .GroupBy(row => new { Field0 = row[excelField[0]], Field1 = row[excelField[1]] })
    .Where(group => group.Count() > 1)
    .Select(g => g.Key);

foreach (var duplicateValuesRow in duplicateValues)
{
    dataTableDuplicateRows.Rows.Add(duplicateValuesRow.Field0, duplicateValuesRow.Field1);
}

1 Answers1

0

I think what you require is something make the linq more dynamic, even though you could achieve it by using expression tree, the DynamicLinq library would appear to solve your issue in an easier way.

For you case, with the library, just use the GroupBy extension method with a string value. More info about DynamicLinq library:

Scott Gu's blog

Panda Zhang
  • 483
  • 3
  • 8