0

I am trying to return an `array with distinct records. But this piece of code isn’t working. What am I doing wrong?

return table.AsEnumerable().Distinct(DataRowComparer.Default).ToArray(); 
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26

1 Answers1

0
table
    .AsEnumerable()
    .GroupBy(row => row.Field<DataType>("FieldName"))
    .Select(group => group.First())
    .ToArray()
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • I assumed OP wants to get distinct by one field, since the question was ambiguous, lot of assumptions. – Suresh Kaushik Apr 16 '19 at 22:17
  • 1
    That's not what I meant. I meant that it this would be a better answer if instead of just a line of code, you provided some explaination as to why this code answers the question. – pppery Apr 16 '19 at 22:18
  • Alright, I am sorry, I suppose it is straight forward, if OP wants to get distinct by one field, then group by that field, and get distinct groups. – Suresh Kaushik Apr 16 '19 at 22:19