none
How to Compare 2 Lists of Object Array List<object[ ]>

    Question

  • I have 2 lists say List1 and list 2. which inturn consists of Array of objects like List<object[ ]>. So my Compare function would be somthing like this  public bool CompareListObjects(List<object[]> Amalgalist, List<object[]> SqlList) How to compare each item and return true of false ?
    Thursday, June 30, 2011 6:25 AM

Answers

  • Hi

    Are you looking for this

     bool Compare(List<object[]> firstList, List<object[]> secondList)
        {
          foreach (var f in firstList)
          {
            foreach (var s in secondList)
            {
              if (!IsobjectListSame(f, s))
              {
                return false;
              }
            }
          }
          return true;
        }
    
        bool IsobjectListSame(object[] f, object[] s)
        {
          //if length not same
          if (f.Length != s.Length)
          {
            return false;
          }
    
          bool matched = true;
          //if length same check 1-1 same or not
          for (int i = 0; i < f.Length; i++)
          {
            if (!f[i].Equals(s[i]))
            {
              matched = false;
              break;
            }
          }
          return matched;
        }
    


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Proposed as answer by Cookie Luo Tuesday, July 05, 2011 3:11 AM
    • Marked as answer by Cookie Luo Sunday, July 10, 2011 12:39 PM
    Thursday, June 30, 2011 8:22 AM
  • Can easily do this using Enumerable.SequenceEqual<TSource> method. Example is here... http://msdn.microsoft.com/en-us/library/bb342073.aspx 

    Thanks


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    • Proposed as answer by Cookie Luo Tuesday, July 05, 2011 3:11 AM
    • Marked as answer by Cookie Luo Sunday, July 10, 2011 12:39 PM
    Thursday, June 30, 2011 9:37 AM

All replies

  • Hi

    Are you looking for this

     bool Compare(List<object[]> firstList, List<object[]> secondList)
        {
          foreach (var f in firstList)
          {
            foreach (var s in secondList)
            {
              if (!IsobjectListSame(f, s))
              {
                return false;
              }
            }
          }
          return true;
        }
    
        bool IsobjectListSame(object[] f, object[] s)
        {
          //if length not same
          if (f.Length != s.Length)
          {
            return false;
          }
    
          bool matched = true;
          //if length same check 1-1 same or not
          for (int i = 0; i < f.Length; i++)
          {
            if (!f[i].Equals(s[i]))
            {
              matched = false;
              break;
            }
          }
          return matched;
        }
    


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Proposed as answer by Cookie Luo Tuesday, July 05, 2011 3:11 AM
    • Marked as answer by Cookie Luo Sunday, July 10, 2011 12:39 PM
    Thursday, June 30, 2011 8:22 AM
  • Hi,

    What do you mean by compare? or what will you be comparing?

    Are you comparing whether the two objects are the same instance? are you comparing whether the two objects are the same type? are you comparing whether the two object have the same unique ID? ....

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Thursday, June 30, 2011 9:34 AM
  • Can easily do this using Enumerable.SequenceEqual<TSource> method. Example is here... http://msdn.microsoft.com/en-us/library/bb342073.aspx 

    Thanks


    Anirban Bhattacharya (Lead Consultant - ESSPL)
    • Proposed as answer by Cookie Luo Tuesday, July 05, 2011 3:11 AM
    • Marked as answer by Cookie Luo Sunday, July 10, 2011 12:39 PM
    Thursday, June 30, 2011 9:37 AM