Search code examples
c#arrayslinqcomparison

C# Jagged Array, determine if any pair matches


In the code below I am populating a Jagged array full of keys(properties) in an object:

  foreach (var item in Items.Where(x => x.NeedsSaved))
        {
            int[][] valuePairs = new int[item.Collection.Count()][];

            int i = 0;

            foreach (var collectionItem in item.Collection)
            {
                valuePairs[i] = new int[4] { collectionItem.Id1, collectionItem.Id2, collectionItem.Id3, collectionItem.Id4};
                i++;
            }
                 -- TODO
         }

        return false;

I am trying to determine if any duplicate key pairs exist. I'm sure there is something easy I am missing with Linq. Every pair of 4 values needs to be unique, so If I have a collection of two in the array that are equal to :

{1, 1, 1, 1} and {1, 1, 1, 1}

The method should return true.

but if the values are {1, 1, 1, 1} {2, 1, 1, 1}

The method should return false.

To add further clarification, the purpose is that when saving in a Client, I am posting to the API. This object has a compound primary key of (a, b, c, d) so I am trying to warn the user by validation before the API is hit, causing a duplicate key exception.

I appreciate any help, and if this is a duplicate I apologize, I tried searching as best as I could for an answer.


Solution

  • Per Xaver's suggestion, I realized that using a Jagged array made things more complicated.

    I ended up using the IEquatable interface, and the solution ended up looking like this:

       private bool HasDuplicateKeyValues()
        {
            foreach (var item in Items.Where(x => x.NeedsSaved))
            {
                List<KeyValues> keyPairs = new List<KeyValues>();
    
                foreach (var collectionItem in item.Collection)
                {
                    keyPairs.Add(new KeyValues(collectionItem.Id1, collectionItem.Id2, collectionItem.Id3, collectionItem.Id4));
                }
    
                foreach (var kp in keyPairs)
                {
                    if (keyPairs.Where(x => x.Equals(kp)).Count() > 1)
                    {
                        return true;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            return false;
        }
    }
    
    public class KeyValues : IEquatable<KeyValues>
    {
        public int Id1 { get; set; }
        public int Id2 { get; set; }
        public int Id3 { get; set; }
        public int Id4 { get; set; }
    
        public KeyValues(int id1, int id2, int id3, int id4)
        {
            Id1 = id1;
            Id2 = id2;
            Id3 = id3;
            Id4 = id4;
        }
    
        public bool Equals(KeyValues other)
        {
            return this.Id1 == other.Id1 && this.Id2 == other.Id2 && this.Id3 == other.Id3 && this.Id4 == other.Id4;
        }
    }
    

    Thank you for your help!