Compare two list and Get boolean values from first List using LINQ [Resolved]

Posted by Allemahesh under LINQ on 10/19/2015 | Points: 10 | Views : 662 | Status : [Member] [MVP] | Replies : 8
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 = 2, Name = "Person 2"},
new Person {Id = 3, Name = "Person 3"},
new Person {Id = 4, Name = "Person 5"},
};


Now I want a LINQ query to compare these two lists such that the name in the two list should be same then return true else false.
Above example will return false as Name = 'Person 4' and Name ='Person 5' is different in two list.
Can any one help me.




Responses

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

Up
2
Down

Resolved
You can also use EXCEPT

 var flag = persons1

.Select(n => n.Name)
.Except(persons2.Select(n => n.Name))
.Count() == 0 ? true : false;

if (flag) Console.WriteLine("Names are equal");
else Console.WriteLine("Names are not equal");


--
Thanks & Regards,
RNA Team

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

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

Up
0
Down

Resolved
Try this
 var flag = persons1

.Where(x1 => !persons2.Any(x2 => x1.Name == x2.Name))
.Union(persons2.Where(x1 => !persons1.Any(x2 => x1.Name == x2.Name)))
.Count() == 0 ? true : false;

if (flag) Console.WriteLine("Names are equal");
else Console.WriteLine("Names are not equal");


--
Thanks & Regards,
RNA Team

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

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

Up
0
Down

Resolved
Also try with SequenceEqual after maintaining the sort

var flag = persons1.Select(n => n.Name).OrderBy(o=>o)
.SequenceEqual(persons2.Select(n => n.Name).OrderBy(o => o));

if (flag) Console.WriteLine("Names are equal");
else Console.WriteLine("Names are not equal");




--
Thanks & Regards,
RNA Team

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

Posted by: Sheonarayan on: 10/19/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
I was thinking to response and then saw your response. The EXCEPT method works like a charm. You are always faster.

Keep it up Rajnilari2015.

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

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

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

Up
0
Down
Thank you Sir but I am your follower.

--
Thanks & Regards,
RNA Team

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

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

Up
0
Down
Long time back I wrote an article on Compare Collections in C# ( http://beyondrelational.com/modules/2/blogs/80/posts/10652/compare-collections-in-c.aspx ) .You can refer that also for more ways to figure out the solution to the problem.

--
Thanks & Regards,
RNA Team

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

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

Up
0
Down
Thank you for your replay. Also I want to compare the Id as well as name.
Can you please update the LINQ query.

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

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

Up
0
Down
@Allemahesh, glad that it has helped you. I have answered based on question you asked for.
However, here is an updated solution for your problem.

var flag = persons1.Where(x1 => !persons2.Any(x2 => x1.Id == x2.Id

&& x1.Name == x2.Name ))
.Union( persons2.Where(x1 => !persons1.Any(x2 => x1.Id == x2.Id
&& x1.Name == x2.Name )))
.Count() == 0 ? true : false;

if (flag) Console.WriteLine("Names are equal");
else Console.WriteLine("Names are not equal");


Case 1: ID and Names are same
Input
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 = 2, Name = "Person 2"},

new Person {Id = 3, Name = "Person 3"},

new Person {Id = 4, Name = "Person 4"},

};


Output
Names are equal


Case 2: ID same but Name different
Input
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 45"}

};



List<Person> persons2 = 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"},

};


Result
Names are not equal

Case 3: ID different but Names are same
Input
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 = 2, Name = "Person 2"},

new Person {Id = 3, Name = "Person 3"},

new Person {Id = 44, Name = "Person 4"},

};


Result
Names are not equal
Hope this helps

--
Thanks & Regards,
RNA Team

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

Login to post response