Compare two list row by row and return boolen value [Resolved]

Posted by Allemahesh under LINQ on 10/28/2015 | Points: 10 | Views : 550 | Status : [Member] [MVP] | Replies : 4
I have a two IList say

List<Person> persons1 = new List<Person>
{
new Person {Id = 1, Name = "Person 1"},
new Person {Id = 2, Name = "Person 2"},
new Person {Id = 3, Name = "Person 3"},
new Person {Id = 4, Name = "Person 4"}
};

List<Person> persons2 = new List<Person>
{
new Person {Id = 1, Name = "Person 1"},
new Person {Id = 8, Name = "Person 8"},
new Person {Id = 3, Name = "Person 3"},
new Person {Id = 4, Name = "Person 5"},
};


If you see these two lists, 2 records are same and 2 are different. Now I want to loop these list and return true or false if bother are same.
For example:

new Person {Id = 1, Name = "Person 1"} == new Person {Id = 1, Name = "Person 1"} => true
new Person {Id = 2, Name = "Person 2"} == new Person {Id = 8, Name = "Person 8"} => false
new Person {Id = 3, Name = "Person 3"} == new Person {Id = 3, Name = "Person 3"} => true
new Person {Id = 4, Name = "Person 4"} == new Person {Id = 4, Name = "Person 5"} => false

Here I will 4 results with 2 are true and 2 are false.
Please help to do this using LINQ query.




Responses

Posted by: Sheonarayan on: 10/28/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Assuming both List are of same length. In this case, first take the length of the list.

var listLength = persons1.Count();
for (int i = 0; i < listLength; i++)
{
if (persons1[i].PropertyName ==persons2[i].PropertyName)
{
// equals
}
else
{
// this item is not equal
}
}


This is little lengthy way but I think in your requirement where you need to get true/false for each item in the collection, this would fit best.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Posted by: Allemahesh on: 10/28/2015 [Member] [MVP] Silver | Points: 25

Up
0
Down
Thank you. But I want to do it with LINQ query.
Can you help me.

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

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

Up
0
Down
Try this


persons1.Select((x, i) => new { RowNum = i + 1, Id = x.Id, Name = x.Name })
.Join(persons2.Select((x, i) => new { RowNum = i + 1, Id = x.Id, Name = x.Name }),
a => a.RowNum, b => b.RowNum,
(a, b) => new { Person1 = a, Person2 = b})
.Select(p => p.Person1.Id == p.Person2.Id && p.Person1.Name == p.Person2.Name)
.ToList()
.ForEach(i => Console.WriteLine(i));


--
Thanks & Regards,
RNA Team

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

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

Up
0
Down
B/T, what happen to your question http://www.dotnetfunda.com/forums/show/20736/show-some-content-of-textbox-as-using-jquery

The solution that we provided didn't worked...right ?

Have some courtesy , by responding to the forums thread so that when another user have a similar kind of doubt we can point the same to him/her. That will save the time of a new discussion for an already old similar kind of discussion.

I hope my words will be granted in a constructive way.

--
Thanks & Regards,
RNA Team

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

Login to post response