I think that your parentheses are not where you think they are. Specifically, a.IsEnabled
is inside the Any
statement, which is then negated.
allRecordsFromB.Any(b => ((b.Id == a.Id) && a.IsEnabled))
This gives you very different logic from your second example. I'd move a.IsEnabled
to the start of your Where
and remove some unnecessary parentheses to clarify things.
var newRecords = allRecordsFromA.Where(a => a.IsEnabled &&
!allRecordsFromB.Any(b => b.Id == a.Id)).ToList();
Note that if you have large data sets, or think the following approach would be clearer, you can speed things up by making a hash set of the b.Id
s. (or via some sort of join, which internally would do the same sort of thing)
var bIds = new HashSet<int>(allRecordsFromB.Select(b => b.Id));
var newRecords = allRecordsFromA.Where(a => a.IsEnabled &&
!bIds.Contains(a.Id)).ToList();