Jay
Jay

Reputation: 634

Linq FindAll and Where returns different results

I have two different types of objects that have ID fields with potentially matching IDs. The FindAll operation returns the correct non-matching objects whereas the Where operation returns all objects. Can someone help me understand why?

var _kenticoIDs = new HashSet<string>(_kenticoSessions.Select(p => p.AttendeeInteractiveSessionID));
var list = _aiSessionIDList.FindAll(p => !_kenticoIDs.Contains(p.SessionID));
var ienum = _aiSessionIDList.Where(p => !_kenticoIDs.Contains(p.SessionID));

EDIT: If I perform a .ToList() on the resultA variable then resulting list is the same as the result variable. However when I inspect the two variables (result/resultA) before the .ToList() one has 6 values and one has 63 values. I feel like I'm missing something obvious.

Upvotes: 1

Views: 1209

Answers (2)

Jaime Torres
Jaime Torres

Reputation: 10515

I think the issue you are having is understanding Linq. Where is a Linq extension method whereas FindAll is a List method. Linq expressions are not evaluated until they are enumerated over, or turned into a list/array.

Upvotes: 2

Channs
Channs

Reputation: 2101

Where and FindAll are equivalent, except that in terms of execution, Where is deferred, but FindAll is immediate.

Source: This SO thread.

Upvotes: 5

Related Questions