I'm running the following expression to get the delta between 2 collections based on the Id
and also make sure the overall result includes only 'Active/Enabled' records:
//Returns the right records but all IsEnabled = false
var newRecords = allRecordsFromA.Where(a => !(allRecordsFromB.Any(b => ((b.Id == a.Id) && a.IsEnabled)))).ToList();
The above result returns all the right records in A
that are not in B
, but all of the records are marked IsEnabled = false
. The really confusing part is I use the exact same expression on 2 other collections in another part of the application and it works. I get back all the proper records and they are all marked as .IsEnabled == true
That makes no sense to me. I even copied over and it doesn't work.
However, if I change to a 2 step process I get the results I desire:
//Works
var newRecords = allRecordsFromA.Where(a => !(allRecordsFromB.Any(b => (b.Id == a.Id)))).ToList();
var results = newRecords.Where(x => x.IsEnabled).ToList();
I was thinking the order of operations in the 1st expression is wrong returning the records with the !
being applied to IsEnabled
but I don't see it. What is incorrect in my 1st expression that is returning records with IsEnabled == false
?