That code was mine and as we established in our comment discussion, replacing the
==
operator by the
.Equals
method works.
For future reference, here's
why that worked:
The return type of
x["Department"]
and
dt.Rows[i]["Department"]
is
object
(and not string, because the table can contain other data as well). When
==
is used on
object
, this is the equivalent of calling
Object.ReferenceEquals[
^] - here it doesn't matter that the actual type is a string, Object.ReferenceEquals is still used (and that's what I forgot when writing the original code). If you use
.Equals
however, then it
will use
String.Equals[
^] (which is what you want), because you have a string and the String type overrides Object.Equals.