views:

27

answers:

1

I have a table GameVersion with FK (allowing nulls) to Game table. When I do this:

    GameVersion[] q = (from gv in db.GameVersion.Include("Game")
                       select gv).ToArray();

It works OK, while iterating GameVersion objects I can see null references to Game in some records (just like on the databse), so it works like left join.

However, when I slightly modify the query and add searching by game name

    GameVersion[] q = (from gv in db.GameVersion.Include("Game")
                       where gv.Game.DisplayName.Contains("a")
                       select gv).ToArray();

It suddenly behaves like inner join (no longer selects null references). I don't quite understand this behaviour. Why is this happening and how do I make the query work? I want to select all GameVersions, even the ones with Game==null plus apply a condition on one of the Game columns.

+5  A: 

Well if there's no game, there can't be a display name, so it can't contain "a". If you want to also select game versions where there's no game, you need to do so explicitly. Try this:

GameVersion[] q = (from gv in db.GameVersion.Include("Game")
                   where gv.Game == null || gv.Game.DisplayName.Contains("a")
                   select gv).ToArray();
Jon Skeet
199,514! you might break 200k today then...
hawk
Yup. I'm stupid. :) Nevermind.
PawelRoman
@hawk: Nah, it won't be today. Tomorrow if I'm lucky though.
Jon Skeet