At first, I thought that Dictinct
would compare by reference, since the DataRow
index returns an Object
instead of an int
. Therefore, I suggested using the strongly typed Field
extension method. But that was not the cause of your problem.
So, until you know the reason, I offer you a different (more efficient) approach to check if all IDs are unique:
var ids = rows.Select(r => r.Field<int>("ID")); var duplicateIdChecker = new HashSet<int>(); bool isIdUnique = ids.All(duplicateIdChecker.Add);
source share