Linq: Join or where conditions ignored

As a part of my effort to start writing more tests for my code, I'm using a mocked DataContext which contains Lists of dummy data for use in unit tests.

However, I'm running into the awkward situation where the method I'm testing returns the correct results, but the test code that verifies these results against the dummy DataContext doesn't.

The method I'm testing is: var earns = earnService.GetEarnsForCharacter(character.Id);

This returns a list of denormalized Earn objects with only Id, Title, and Display properties. The actual data entities have many more properties, including the one I'm testing for, which is WorldId. I want the WorldId of the returned Earn objects to be equal to character.WorldId.

I figured I should be able to do something like this:

// Execute the method we're testing
var earns = earnService.GetEarnsForCharacter(character.Id);

// Assert that the character's world id is indeed equal to the world id's of all the selected earns
// First, fetch the earns from the datacontext
var worldIds = from ec in earns join e in mockedDataContext.Object.Get<Earn>() on ec.Id equals e.Id select e.WorldId;
foreach (var worldId in worldIds)
    Assert.AreEqual(character.WorldId, worldId);

However, for some reason, while the earns variable contains the expected 2 objects, the worldIds enumerable contains 3 objects, no matter what I do. I have tried adding a where clause with where earns.Select(t => t.Id).Contains(e.Id) as well, but this has absolutely no effect on the result either.

Any suggestions are much appreciated!


If mockedDataContext.Object.Get<Earn>() returns two items with the same Id value then your join will return 3 records instead of the expected two.

Need Your Help

Line break on keydown

javascript jquery textarea onkeydown

I have a textarea, and on each enter i want it to get blank if something has written. but my problem is; on the first enter it line breaks, and you continue to write from the second line. it only h...

WP8.1 back button quits the app

c# xaml windows-phone-8 mvvm-light

The last time I developed against Windows Phone, it was with version 8. Now I'm making use of 8.1. Maybe this is a new feature from MS, but when I press the back button on the phone, regardless of ...