Lambda Where Id Does Not Exist in Another List

LINQ Lambda - Find all ID's in one list that don't exist in another list

If you only need the IDs of the items then Mark's answer will do the trick nicely. If you need to return the items themselves (and they don't already have a suitable Equals implementation) then you could try something like this:

// assumes that the ID property is an int - change the generic type if it's not
var ids = new HashSet<int>(list1.Select(x => x.ID));
var results = list2.Where(x => !ids.Contains(x.ID));

Use LINQ to get items in one List<>, that are not in another List<>

This can be addressed using the following LINQ expression:

var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));

An alternate way of expressing this via LINQ, which some developers find more readable:

var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID));

Warning: As noted in the comments, these approaches mandate an O(n*m) operation. That may be fine, but could introduce performance issues, and especially if the data set is quite large. If this doesn't satisfy your performance requirements, you may need to evaluate other options. Since the stated requirement is for a solution in LINQ, however, those options aren't explored here. As always, evaluate any approach against the performance requirements your project might have.

Get List of Records that do not exist in another list - Linq

I think you need to negate your logic. Your current logic will always return true, because the dsUserList most likely has an entry where the GlobalIdentity does not match your dbUserList entry. So, if there are any in the dsUserList where there's a match, exclude them:

var disableUserList = dbUserList.Where(ds => !dsUserList.Any(db => db.GlobalIdentity == ds.GlobalIdentity)).ToList();

C# Lambda to filter list based on existence on another list

it should be "ALL", or "Not Any"

foreach(var item in ListA.Where(x=>ListB.All(b=>x.ID != b.ID)))
{
//Here I should perform operation with item that having ID 7 and 9
}

update:

As you actually want to have distinct result from A except B, so, you can do either:

foreach(var item in ListA.GroupBy(m=>m.ID).Where(x=>ListB.All(b=>b.ID != x.Key)))
{
counter ++;
Debug.writeline(item.Key);
}

or

foreach(var id in ListA.Select(x=>x.ID).Distinct().Except(ListB.Select(y=>y.ID)))
{
counter++;
}

note: all untested - i have no compiler with me for the moment.

LINQ - Find all items in one list that aren't in another list

Try using .Except extension method (docs):

var result = list1.Except(list2);

will give you all items in list1 that are not in list2.

IMPORTANT: Even though there's a link provided to MSDN docs for the method, I'll point this out here: Except only works out of the box for collections of primitive types, for POCOs/objects you need to implement IEquatable on that object.

How to remove value that does not exist in another list?

First, let's fix your code - you remove when any item in the update matches master, so != should be ==:

master.RemoveAll(c => !update.Any(x => x.Id == c.Id));

That's all you need for a list of, say, 1000 items or so. If the list is, say, 10,000 items, this could become slow due to O(n2) nature of the above algorithm. You can put IDs of update into a HashSet, and use Contains for a potential speed-up:

var updateIds = new HashSet<int>(update.Select(u => u.Id));
master.RemoveAll(m => !updateId.Contains(m.Id));


Related Topics



Leave a reply



Submit