2009-12-02 35 views
1

我有一套我需要選擇不同的任務(在L2E中分組)。這些任務有一個引用屬性,我需要加載「Activity」,以便我可以引用task.Activity對象。在下面的第一個例子中,這工作正常。但是,當我按描述進行分組時,活動並未加載。我錯過了什麼?LINQ to Entities - 「包括」一個複雜的對象,同時也分組

tasks = 
    (from x in Db.Task.Include("Activity") select x) 
    .ToList(); 

tasks = 
    (from x in Db.Task.Include("Activity") 
    group x by x.Description 
    into groupedByDescription 
     select groupedByDescription.FirstOrDefault()).ToList(); 

回答

2

groupedByDescription不是實體;它是一組實體。當您選擇一個實體時,您只能使用Include

更多的信息是in this tip

很難給出一個解決方法,不知道你想解決什麼問題。

0

我想解釋一下這個問題,因爲我認爲這個問題沒有得到解答。

基於例如從這裏: 的IQueryable> prodQuery = 從db.Products PROD 組由prod.CategoryID刺到分組 選擇分組;

foreach (IGrouping<Int32, Product> grp in prodQuery) 
{ 
    Console.WriteLine("\nCategoryID Key = {0}:", grp.Key); 
    foreach (Product listing in grp) 
    { 
     Console.WriteLine("\t{0}", listing.ProductName); 
    } 
} 

如果表產品具有表T_Category一個外鍵關係,訪問類別的唯一方法是通過db.Product.T_Category.CategoryID。如果是這種情況,是否會包含能夠獲取T_Category中其他信息的工作,例如T_Category.CategoryName?

的IQueryable> prodQuery = 從db.Products.include( 「T_Category」)由prod.T_Category.CategoryID 組督促刺到分組 選擇分組;

foreach (IGrouping<Int32, Product> grp in prodQuery) 
{ 
    Console.WriteLine("\nCategoryID Key = {0}:", grp.Key); 
    foreach (Product listing in grp) 
    { 
     Console.WriteLine("\t{0}", listing.ProductName); 
     Console.WriteLine("\t{0}", listing.T_Category.CategoryID); 
     Console.WriteLine("\t{0}", listing.T_Category.CategoryName); 
    } 
}