Reputation: 9876
At runtime I'm getting two different arrays {object[10]}
and I would like to check if the values in the first array are the same as those in the other. The actual types of the elements could be string
, int
or bool
. For example element [1] = "Test"
and element [2] = 3
and so on.
What I've done is this:
for (var j = 0; j < newData.ItemArray.Length; j++)
{
if (newData.ItemArray[j].ToString().ToLower() != originalData.ItemArray[j].ToString().ToLower())
{
isModified = true;
break;
}
}
I can't say that I'm satisfied with this solution, however it seems to work judging by the few tests I made. However I feel that there should be a better way for doing this.
ADDITIONAL Judjing from the comments maybe I wasn't too clear in my question. This is what I get as input:
I expect the other array to contains the same data. The only problem is that all elements are stored as objects. So for example I would like to know if element [3]
in the first array is like element [3]
from the another array. Ideally I would like to compare two boolean values but since everything is stored as objects I'm looking for ideas how to check if the values are the same or for example from [1] = "Training"
in the other array is [1] = "Not Training"
and so on..
Upvotes: 1
Views: 2776
Reputation: 460158
Since you use ItemArray
i'm pretty sure that it's the property of DataRow
which returns a Object[]
of all fields of this row. So you want to compare two datarows with each other.
You can use SequenceEqual
:
bool isModifed = !newData.ItemArray.SequenceEqual(originalData.ItemArray);
This compares strings case-sensitively as opposed to your approach.
If you want to ignore the case:
isModifed = !newData.ItemArray.Select(obj => obj?.ToString())
.SequenceEqual(originalData.ItemArray.Select(obj => obj?.ToString())
, StringComparer.InvariantCultureIgnoreCase);
Upvotes: 5