LINQ - Left join with LinqToEntities using POCO in EF4
I am having alot of troubles with my JOIN, I dont know if it depends on the EDM or if its just the LINQ-query.
When I try this T-SQL in SSMS it works like a charm:
select g.Id, g.Title, p.Platform_short from games as g left outer join Platforms as p on g.PlatformId = p.Id
but when I try to translate the T-SQL to LINQ, with this:
The problem I get is that the Platform == null
I have tried different approaches but then I get duplicates.
I've been stuck with this problem for a couple of days now, and I'm new to EF.
var games = (from g in ctx.Games
join p in ctx.Platforms
on g.Platform.Id equals p.Id
select new
{
Id = g.Id,
Title = g.Title,
Platform = g.Platform,
});
Heres is the whole method:
public ICollection<Game> GetGames()
{
using(xContext ctx = new xContext())
{
ICollection<Game> col = new Collection<Game>();
var games = (from g in ctx.Games
join p in ctx.Platforms
on g.Platform.Id equals p.Id
select new
{
Id = g.Id,
Title = g.Title,
Platform = g.Platform,
});
foreach (var g in games)
{
Game game = new Game();
game.Id = g.Id;
game.Title = g.Title;
game.Platform = g.Plat开发者_StackOverflow社区form;
col.Add(game);
}
return col;
}
}
My GamePoco is having a Platform-property and is one-to-one relationship in the EDM.
Hope anyone can help!
What is purpose of your left join when result doesn't have anything from joined enity set?
Try this:
var games = (from g in ctx.Games
join p in ctx.Platforms
on g.Platform.Id equals p.Id into joinTable
from j in jointTable.DefaultIfEmpty()
select new
{
Id = g.Id,
Title = g.Title,
Platform = j.PlatformShort
});
This works only in EFv4.
精彩评论