2014-01-06 56 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