2014-01-06 54 views
0

我有一個擁有類別的類。使用linq添加項目列表

public class CategoryDto 
{ 
    public int Id { get; set; } 
    public int PortfolioId { get; set; } 
    public string Description { get; set; } 
    public List<SubCategoryDto> SubCategories { get; set; } 

    public CategoryDto() 
    { 
     SubCategories = new List<SubCategoryDto>(); 
    } 
} 

它裏面有一個列表,這是其他子類別類的列表:

public class SubCategoryDto 
{ 
    public int Id { get; set; } 
    public int CategoryId { get; set; } 
    public CategoryDto Category { get; set; } 
    public string Description { get; set; } 
} 

我然後填充這個項目,但我得到一個「PortfolioId」列表基地。

var cats = (from p in Context.transaction_category 
        where p.account_portfolio_id == portfolioId 
          && p.deleted == null 
        select new CategoryDto 
         { 
          Id = p.id, 
          Description = p.description, 
          PortfolioId = p.account_portfolio_id 
         }).ToList(); 

Categories表具有SubCategories的外鍵。每個類別都有0:n個子類別。所以,實體框架模型有一個Context.transaction_category.transaction_sub_categories集合。

因此,現在我所做的是,通過上面列表中的類別進行foreach,並填充子類別。

有沒有辦法在同一個鏈接語句中做到這一點? Categories對象有一個List列表。它可以在上面的Linq聲明中完成嗎?

編輯: 這是修復的嘗試,作爲推薦,但呈現的錯誤:

var cats = (from p in Context.transaction_category 
         where p.account_portfolio_id == portfolioId 
           && p.deleted == null 
         select new CategoryDto 
          { 
           Id = p.id, 
           Description = p.description, 
           PortfolioId = p.account_portfolio_id, 
           SubCategories = (from s in Context.transaction_sub_category where s.transaction_category_id == p.id 
               && s.deleted == null 
                select new SubCategoryDto 
                 { 
                  Id = s.id, 
                  Description = s.description, 
                  CategoryId = s.transaction_category_id 
                 }).ToList() 
          }).ToList(); 

LINQ to Entities does not recognize the method 'System.Collections.Generic.List 1[Objects.SubCategoryDto] ToList[SubCategoryDto](System.Collections.Generic.IEnumerable 1[Objects.SubCateg‌​oryDto])' method, and this method cannot be translated into a store expression.

+0

哪裏是你的子類別?你能分享你的子類嗎? –

+1

添加了要發佈的子類。 – Craig

回答

1

你可以這樣說:

var cats = (from p in Context.transaction_category 
       where p.account_portfolio_id == portfolioId 
         && p.deleted == null 
       select new CategoryDto 
        { 
         Id = p.id, 
         Description = p.description, 
         PortfolioId = p.account_portfolio_id, 
         SubCategories = (from s in Context.transaction_category.transaction_sub_categories 
          where s.CategoryId == p.Id 
           select new SubCategoryDto { 
            Id = s.Id, 
            Description = s.Decription  
         }).ToList() 
        }).ToList(); 

更新:爲了使它更容易改變你的SubCategoriesCategory這樣的屬性:

public virtual List<SubCategoryDto> SubCategories { get; set; } 

public virtual CategoryDto Category { get; set; } 

然後你可以使用包括,簡單地加載子類是這樣的:

var cats = Context.transaction_category 
       .Where(p => p.account_portfolio_id == portfolioId && p.deleted == null) 
       .Include(p => p.SubCategories); 
+0

謝謝。這應該工作。只是出於興趣......會導致SQL做某種子查詢(慢),或者它會使用INNER JOIN來獲取相關子查詢的數據嗎? – Craig

+0

我不確定你可以用sql profiler來檢查它 –

+0

不,它不。每次構建/選擇一個'CategoryDto'時,它都會往返數據庫。因此,如果你有100個類別,你將會遇到數據庫101次 – Leo