1

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:

enter image description here

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..

5
  • Override the Object Equals(this Object object) method and compare each property. Commented Jan 27, 2017 at 13:38
  • @Leron: I've just asked because of your bad example with only strings. That's why i've modified it Commented Jan 27, 2017 at 13:39
  • If there is an string like "1" and an integer with value 1 (just as an example) your function will return an ok incorrectly. Commented Jan 27, 2017 at 13:39
  • Bear in mind that if string case is important then this will fail because "FOO" = "foo" Commented Jan 27, 2017 at 13:41
  • If you don't mind LINQ: SequenceEqual should work for primitive types. Commented Jan 27, 2017 at 13:43

1 Answer 1

5

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); 
Sign up to request clarification or add additional context in comments.

6 Comments

SequenceEquals() will compare using Equals operator which in most cases ( for user defined types ) will not be overwritten meaning the comparison will fail.
You are exactly right, I am comparing DataRows. But the elements in the ItemArray are the ones that I really would like to compare.
@Leron: and that is what SequenceEqual does :)
@m.rogalski: it is overwritten for all types that are supported in a DataTable (apart from Byte[]). If you want to support a custom type you either have to override Equals+GetHashCode or pass a custom IEqualityComparer<T> to SequenceEqual. You can implement your own or use an available like i did in the second example.
@TimSchmelter Exactly what I was looking for. Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.