I have a DataTable which I want to check if values in three of the columns are unique. If not, the last column should be filled with the line number of the first appearance of the value-combination.
For example, this table:
ID Name LastName Age Flag
-------------------------------------
1 Bart Simpson 10 -
2 Lisa Simpson 8 -
3 Bart Simpson 10 -
4 Ned Flanders 40 -
5 Bart Simpson 10 -
Should lead to this result:
Line Name LastName Age Flag
-------------------------------------
1 Bart Simpson 10 -
2 Lisa Simpson 8 -
3 Bart Simpson 10 1
4 Ned Flanders 40 -
5 Bart Simpson 10 1
I solved this by iterating the DataTable with two nested for loops and comparing the values. While this works fine for a small amount of data, it gets pretty slow when the DataTable contains a lot of rows.
My question is: What is the best/fastest solution for this problem, regarding that the amount of data can vary between let’s say 100 and 20000 rows?
Is there a way to do this using LINQ? (I’m not too familiar with it, but I want to learn!)
Okay, I think I got an answer myself. Based on the suggestion in James Wiseman’s answer, I tried something with LINQ.
With this query I get exactly the result that’s described in the Question. The
myErrnrFnctFunction is necessary because I want theFlagcolumn to have the value0if there is no other row with the same values.To get a DataTable out of
myQueryagain, I had to add some extensions described here:How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
And then, this line will do:
This seems to work just fine. Any suggestions to do this better?