使用linq添加项目列表

时间:2014-01-06 01:10:43

标签: c# linq entity-framework

我有一个包含类别的课程。

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

它有一个List,它是SubCategory类的列表:

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

“类别”表具有SubCategories的外键。每个类别都有0:n个子类别。因此,实体框架模型具有Context.transaction_category.transaction_sub_categories集合。

所以现在我所做的是,通过上面列表中的类别,并填充子类别。

有没有办法在同一个链接语句中执行此操作? 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无法识别该方法   'System.Collections.Generic.List 1[Objects.SubCategoryDto] ToList[SubCategoryDto](System.Collections.Generic.IEnumerable 1 [Objects.SubCateg oryDto])'   方法,并且此方法无法转换为商店表达式。

1 个答案:

答案 0 :(得分: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; }

然后您可以使用include并简单地加载您的子类别,如下所示:

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