2011-05-16 77 views
1

我有一些設置實體與設置組相關的SettingDescription。 通過使密鑰的「修改」字段部分保留設置歷史記錄。 爲了讓設置匹配我用這個查詢(after help from here)一個特定的類別:如何在分組實體LINQ查詢中包含相關對象?

var latestSettings = 
     context.Settings.Include("Description.SettingGroup") 
      .OrderByDescending(x => x.Modified) 
      .GroupBy(x => 
       new { 
        x.Category, 
        x.Group, 
        x.Name, 
        x.Target }, x => x) 
      .Where(x => x.Key.Category == category) 
      .Select(result => result.FirstOrDefault()) 
      .ToArray(); 

這將返回一組最新的設置,但「包含」部分被完全忽略。但是,我可以強制加載描述,方法是運行第二個虛擬查詢,將查詢加載到上下文中。

var latestSettings = 
     context.Settings.Include("Description.SettingGroup") 
      .OrderByDescending(x => x.Modified) 
      .GroupBy(x => 
       new { 
        x.Category, 
        x.Group, 
        x.Name, 
        x.Target }, x => x) 
      .Where(x => x.Key.Category == category) 
      .Select(result => result.FirstOrDefault()) 
      .ToArray(); 
    var settingDescriptions = 
     context.SettingDescriptions.Include("SettingGroup") 
       .Where(x => x.Category == category) 
       .ToArray(); 

爲什麼在「獨立」組查詢中被忽略的包含?

我可以將設置和描述加載組合到單個查詢中嗎?

回答

相關問題