Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the difference between 2 IEnumerable objects

Tags:

c#

.net

I have 2 list of guids as:

IEnumerable<dynamic> userids = null;
IEnumerable<dynamic> lsCheckedUsers = null;

The userids and lsCheckedUsers list are populated from a SQL database using dapper.

I now wish to find all userids that are not in lsCheckedUsers.

I have tried the following

var userdifference = userids.Where(i => !lsCheckedUsers.Contains(lsCheckedUsers));
var userdifference = userids.Except(lsCheckedUsers);

None of the above actual returns the difference between the 2.

How do I get the difference of guids that do not exist in both.

I am certain that lsCheckedUsers has Guids that are in userids

like image 682
user1526912 Avatar asked Mar 27 '13 18:03

user1526912


2 Answers

This is correct:

var userdifference = userids.Except(lsCheckedUsers);

It will work if both of your IEnumerable<dynamic> actually contain Guids. Print out or inspect the items in each to make sure they are Guids.

You should really be using IEnumerable<Guid> and cast the incoming items to Guids if this is what you are expecting. It will hopefully prevent errors like the one you are potentially seeing.

like image 91
Ian Gardner Avatar answered Oct 30 '22 03:10

Ian Gardner


Something along those lines..

var difference = list1.Where (e => !list2.Any(a => a == e))
like image 28
Mathieu Guindon Avatar answered Oct 30 '22 03:10

Mathieu Guindon