2011-03-15 64 views
0

反正是有,我可以在此查詢得到整個對象平臺而不只是性質的,我使用波科與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         

     }); 

而不是使用PlatformId的和PlatformShort我只想檢索整個Platform對象(j)。像「平臺= j」

有沒有更好的方式來返回類型集合與迭代通過遊戲第一?

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); 
      } 

我嘗試這樣做,而不是:

return ctx.Games.Include("Platform").Take(50).ToList();

,現在我得到了所有100次記錄相同的值。

這裏是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] 
+0

某處有問題,你的應用程序,因爲使用'平臺= j'應該工作。 – 2011-03-15 21:12:18

回答

0

是的,有一個更好的辦法:

var collection = context.Games.Include("Platform").ToList(); 
+0

當我使用你的方法時,我得到這個錯誤: 當沒有數據存在時無效嘗試讀取。 任何想法? – n3tx 2011-03-15 20:33:05

+0

您的模型如何定義。 'Game'上的'平臺'導航屬性?如果是的話,這必須工作,這是正確的方式來做到這一點。 – 2011-03-15 20:34:21

+0

yes Platform是Game上的導航屬性。 也許是要處理很多數據?我有30000多條記錄。 – n3tx 2011-03-15 20:50:02

相關問題