-2

I am trying to compare the arrays present within a dictionary,and pass the keys if arrays are same.I am using syntax something like this to check the values , Can anyone help with the correct syntax.

Dictionary<int, string[]> test = excel.GetSheetColumnPairs(xlWorkbook, myint);
//loop dictionary all elements   
foreach (KeyValuePair<int, string[]> pair in test)
{
    Console.WriteLine(pair.Key + "....." + pair.Value + "<br />");
}
//find dictionary duplicate values.  
var duplicateValues = test.GroupBy(x => x.Value).Where(x => x.Count() > 1);

Console.WriteLine("<br /><b>dictionary duplicate values..........</b><br />");

//loop dictionary duplicate values only            
foreach (var item in duplicateValues)
{
    Console.WriteLine(item.Key + "<br />");
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • Do you get a compiler error? – Code-Apprentice Oct 30 '18 at 15:25
  • 1
    The keys in a dictionary should be unique by definition, you cannot have duplicated keys. So, in your case, if you try to create a dictionary and you have duplicated keys in the excel, then you'll get an exception. My suggestion here will be to read the excel using another collection, like a Tuple or a specific class where you can map the elements from the excel – Guillermo Gerard Oct 30 '18 at 15:32
  • 3
    Please add input and expected output data examples. – Renatas M. Oct 30 '18 at 15:34
  • @Code-Apprentice: Nope i don't get any errors used to get null values. – Suraj Revankar Nov 01 '18 at 08:45
  • @GuillermoGerard:i am not reading the excel here i am passing it to other methods which would return key as sheetid's and value as array of column names, but comparing dictionary value of string arrays we would have to write extra methods/functionalists for that, i replaced string array to string and i could get it give the duplicate values. – Suraj Revankar Nov 01 '18 at 08:48
  • @Renuiz: Examples would be something like dictionary test with {key=1,Value=[item,numbers,model]},{key=2,Value=[item,numbers,model]} etc., and the result to be print keys if the string array values are same. That is print 1 and 2. So it would any generic key value pairs so i thought examples won't be needed. Thanks for the reply – Suraj Revankar Nov 01 '18 at 08:52
  • I Replaced the string[] to string and then i could easily do the grouping and duplicate values, that helped me to get the desired result thanks all for the replies – Suraj Revankar Nov 01 '18 at 08:52

2 Answers2

1

Assuming that the keys are always different and your code don't explode: The GroupBy is using the equality implementation for arrays. By default two arrays will be equal if they are referencing the same object (the same array); i.e: the default equality implementation won't care about the elements of the array, it only test that the referenced object are the same object. If you want to know if two arrays (different references, different objects in memory) contains the same elements, you have to create your own method and compare the elements one by one(or you can override the equality implementation)

Guillermo Gerard
  • 808
  • 10
  • 21
  • Hi Gerard, yes right..i was looking something without writing other methods, so that the code is compact. i ended up using string instead of string arrays. which then i could easily use the groupby properties. – Suraj Revankar Nov 01 '18 at 08:44
0

My understanding is that you are trying to compare the string[] arrays and in case they have the same values print the keys corresponding to the arrays (each array representing a line of values from excel).

However in case of arrays you would need to compare each value rather than the array themselves as these are reference objects and their equality would mean they would reference basically the same area of memory (which is not the case here).

A way to compare two arrays is presented here (and you'd do the comparison for each line with each line). For future reference if you'd need to compare array of objects you may also have a look here.

Sorin87bv
  • 156
  • 8
  • Hi Sorin thanks for the reply , yes i had seen that, here the string array are in the value of dictionary which has to be compared with the other values of the same dictionary(string arrays), i had seen the array comparison, but was not sure how to do within the dictionary values of string arrays. I ended up using only strings instead of string array which then i could use grouping and lookup to find the duplicate values, thanks for the reply :) – Suraj Revankar Nov 01 '18 at 08:40