How can I compare Two List Objects in C# [Resolved]

Posted by Kasani007 under C# on 11/16/2015 | Points: 10 | Views : 4139 | Status : [Member] | Replies : 8
How can I compare Two List Objects In C# Using Linq or Anything

First List Object First Element Should compare only with second List Object First Element i.e.,

List<ActualAddress> aAddress=new List<ActualAddress>
List<CurrentAddress> cAddress=new List<CurrentAddress>

//How can I Compare n number Of Elements Of Both The Objects Frist Element In a Simple Format
aAddress.Address1[0]==cAddress.Address1[0];
aAddress.Address1[1]==cAddress.Address1[1];
aAddress.Address1[2]==cAddress.Address1[2];
------ ------- ------ -----
------- -------- ----- -----
aAddress.Address1[n]==cAddress.Address1[n];


please help me...




Responses

Posted by: Rajnilari2015 on: 11/16/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var aAddress = new List<ActualAddress>()
{
new ActualAddress { Address = "Address1" }
,new ActualAddress { Address = "Address2" }
,new ActualAddress { Address = "Address33" }
,new ActualAddress { Address = "Address4" }
,new ActualAddress { Address = "Address5" }
};

var cAddress = new List<CurrentAddress>()
{
new CurrentAddress { Address = "Address1" }
,new CurrentAddress { Address = "Address2" }
,new CurrentAddress { Address = "Address3" }
,new CurrentAddress { Address = "Address4" }
,new CurrentAddress { Address = "Address6" }
};

var x = aAddress.Select(a => a.Address).Except(cAddress.Select(c => c.Address));
var y = cAddress.Select(a => a.Address).Except(aAddress.Select(c => c.Address));

x.Zip(y, (a, b) => ("Old Address :" + a + " Changes To New Address:" + b))
.ToList()
.ForEach(i => Console.WriteLine(i));

Console.ReadKey();
}
}

public class ActualAddress
{
public string Address { get; set; }
}

public class CurrentAddress
{
public string Address { get; set; }
}
}


Result

Old Address :Address33 Changes To New Address:Address3
Old Address :Address5 Changes To New Address:Address6


--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/16/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try this

var flag = aAddress

.Select(n => n.Address1)

.Except(cAddress.Select(n => n.Address1))

.Count() == 0 ? true : false;



if (flag) Console.WriteLine("Addresses are equal");

else Console.WriteLine("Addresses are not equal");


--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/16/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
A few day back I answered a similar kind of question http://www.dotnetfunda.com/forums/show/20724/compare-two-list-and-get-boolean-values-from-first-list-using-linq

Hope that also helps

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 11/16/2015 [Member] Starter | Points: 25

Up
0
Down
hi Rajnilari,

ThanQ for Your Reply , your reply helped me...

and Also I want To display what the changes are in both the object elements, if the addresses are not same... how can i hold the data and display..i.e., output should be like ...
"The Actual Address1[0] string is:____ and Current Address1[0] string is:..."
"The Actual Address1[1] string is:____ and Current Address1[1] string is:..."
-------------------------------------------- -----------------------------
------------------ -------------- ---------------- -----
"The Actual Address1[n] string is:____ and Current Address1[n] string is:..."
please help me

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 11/16/2015 [Member] Starter | Points: 25

Up
0
Down
thanQ Very Much Rajnilari... its working

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/17/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Glad that it has helped you.In that case could you please accept the answer. (:

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 11/19/2015 [Member] Starter | Points: 25

Up
0
Down
var numbers = new [] { 1, 2, 3, 4 };
var words = new [] { "one", "two", "three", "four" };

var numbersAndWords = numbers.Zip(words, (n, w) => new { Number = n, Word = w });
foreach(var nw in numbersAndWords)
{
Console.WriteLine(nw.Number + nw.Word);
}

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/19/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@ Kasani007, how the above code is related to the question being asked?

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response