A full explanation and reproduction (incl. screenshots, SQL scripts, repro solution, etc) are at: http://stackoverflow.com/questions/12138353 Brief outline: When performing a .FirstOrDefault() on an inner query, instead of returning one inner entity per outer query entity, only one entity in total is returned (as though .FirstOrDefault() was incorrectly lifted to the outer query). This happens despite the correct SQL being generated and the correct number of rows being returned from the database - EF simply ignores returned rows. Replacing .FirstOrDefault() with .Take(1) resolves the issue (but of course produces incorrect collection nesting depth). Please see the above link for a detailed account of this bug and how to consistently reproduce it.
↧