Retrieve whole object in a join instead of just properties
Is there anyway I can get the whole object Platform instead of just the properties in this query, I am using Poco with EF4:
var games = (from g in ctx.Games
join p in ctx.Platforms on g.Platform.Id equals p.Id into joinTable
from j in joinTable.DefaultIfEmpty()
select new
{
GameId = g.Id,
Title = g.Title,
PlatformId = j.Id,
PlatformShort = j.Platform_short
});
Instead of using PlatformId and PlatformShort I just want to retrieve the whole Platform object (j). Like "Platform = j"
And is there any better way to return a typed collection with out iterating through games first ?
foreach (var g in games)
{
Game game = new Game();
game.Platform = new Platform();
game.Id = g.GameId;
game.Title = g.Title;
game.Platform.Id = g.PlatformId;
game.Platform.Platform_short = g.PlatformShort;
col.Add(game);
}
I tried this instead:
return ctx.Games.Include("Platform").Take(50).ToList();
ANd now i got same value for all 100 rec开发者_C百科ords.
Here is the SQL:
FROM [dbo].[Games] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[Id] AS [Id1], [Extent2].[Platform_short] AS [Platform_short], [Extent2].[Platform_long] AS [Platform_long], [Extent2].[Description] AS [Description], [Extent2].[Image] AS [Image], [Extent2].[CreatedDate] AS [CreatedDate1], [Extent2].[ModifiedDate] AS [ModifiedDate1]
FROM [dbo].[Platforms] AS [Extent2]
LEFT OUTER JOIN [dbo].[Games] AS [Extent3] ON [Extent2].[Id] = [Extent3].[PlatformId] ) AS [Join1] ON [Extent1].[PlatformId] = [Join1].[Id1] LEFT OUTER JOIN (SELECT [Extent4].[Id] AS [Id3], [Extent5].[Id] AS [Id2]
FROM [dbo].[Platforms] AS [Extent4]
LEFT OUTER JOIN [dbo].[Games] AS [Extent5] ON [Extent4].[Id] = [Extent5].[PlatformId] ) AS [Join3] ON [Extent1].[PlatformId] = [Join3].[Id3]
Yes there is a better way:
var collection = context.Games.Include("Platform").ToList();
精彩评论