How to find the difference between 2 IEnumerable objects

I have 2 lists of names:

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

The list of users and lsCheckedUsers is populated from the SQL database using dapper.

Now I want to find all userid that are not in lsCheckedUsers.

I tried the following

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

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

How to get the difference between sheets that do not exist in both.

I'm sure lsCheckedUsers have Guids that are in userid

+5
source share
4 answers

It is right:

var userdifference = userids.Except(lsCheckedUsers);

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

IEnumerable<Guid> Guids, , . , , .

+9

- .

var difference = list1.Where (e => !list2.Any(a => a == e))
+2

:

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

, :

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

Update:

, - "" , , Guid , -. , :

var guid = Guid.NewGuid();
var guids = new[] { new Guid(guid.ToString()) };

Console.WriteLine(guids.Contains(guid));

, True.

+1

Except

Enumerable.Except Method (IEnumerable, IEnumerable)

String GUID.
.

HashSet ExceptWith, , .
HashSet, .

HashSet.ExceptWith Method

+1

All Articles