Compare two string arrays using C#.Net


please try this, use linq
string[] array1 = new string[] { "Data", "Account", "credit"
"Debit" };//This array Contain constant vlues
string[] array2 = new string[] { "Data1", "Account1", "credit"
"Debit" };//This array may be same or may be diff
string[] array3 = new string[] { "Data", "Account", "credit",
 "Debit" };//This array Contain constant vlues
//i have to comapre this two arrays ...if two string arrays values same 
//i have to add this below code..
var diff = from item in array1 where array2.Contains(item)
 select item;
if (diff.Count() == array1.Length )
{
 //the same
 MessageBox.Show("array1 vs array2 the same");
}
else
{
 //not the same
 MessageBox.Show("array1 vs array2 not the same");
}
var diff2 = from item in array1 where array3.Contains(item) select item;
if (diff2.Count() == array1.Length)
{
 //the same
 MessageBox.Show("array1 vs array3 the same");
}
else
{
 //not the same
 MessageBox.Show("array1 vs array3 not the same");
}


-------------------------------------------------------------------------------------------------------


int[] array1 = { 1,2,3,4,5 };
      int[] array2 = { 2,5,6,7,8 };

      Console.WriteLine("What array1 has that array2 doesn't:");
      foreach (int i in array1.Except(array2))
      {
        Console.WriteLine(i);
      }

      Console.WriteLine();

      Console.WriteLine("What array2 has that array1 doesn't:");
      foreach (int i in array2.Except(array1))
      {
        Console.WriteLine(i);
      }


-----------------------------------------------------------------------
var list1 = string[] {"1", "2", "3", "4", "5", "6"};
var list2 = string[] {"2", "3", "4"};
listDiff = list1.Except(list2); //This gets the desire result for different items
//This doesn't give me desire result. Comes out as {"1", "5", "6", "2", "3", "4"};


-------------------------------------------------------

foreach (string com in com2 )
{
    if (!com1.Contains(com))
    {
        MessageBox.Show(com);
    }
}


0 comments:

Post a Comment