带有急切加载的子集合的EF Linq Query Collection

时间:2011-07-22 09:10:14

标签: .net c#-4.0 linq-to-entities ef4-code-only

我首先使用EF4.1代码,

我正在尝试创建一个返回带有急切加载的子集合的集合的查询。 两者都必须按位置 IsActive == true

排序

映射这些类。

public class Type
{
    public int Id { get; set; }
    public int AreaId { get; set; }
    public bool IsActive { get; set; }
    public int Position { get; set; }
    .......
    public virtual IList<Category> Categories { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public int TypeId { get; set; }
    public bool IsActive { get; set; }
    public int Position { get; set; }
    .......
    public virtual Type Type { get; set; }
}

的DTO:

    public class TypeDTO
    {
        public string Name { get; set; }
        public string UniqueName { get; set; }
        public virtual IList<CategoryDTO> Categories { get; set; }
    }

public class CategoryDTO
    {
        public string Name { get; set; }
        public string UniqueName { get; set; }

        public virtual TypeDTO Type { get; set; }
    }

我得到了什么:

使用DTO:

var result = _db.Types
                             .Where(x => x.IsActive == true)
                             .Where(x => x.AreaId== areaId)
                             .Select(x => new TypeDTO()
                             {
                                 Name = x.Name,
                                 UniqueName = x.UniqueName,
                                 Categories = x.Categories.Where(c => c.IsActive == true)
                                                          .OrderBy(c => c.Position)
                                                          .Select(c => new CategoryDTO()
                                                          {
                                                              Name = c.Name,
                                                              UniqueName = c.UniqueName
                                                          })
                                                          .ToList()

                             })
                             .ToList();

这引发:

  

LINQ to Entities无法识别该方法   'System.Collections.Generic.List 1[...CategoryDTO] ToList[...CategoryDTO](System.Collections.Generic.IEnumerable 1 [... CategoryDTO])'   方法,并且此方法无法转换为商店表达式。

没有DTO:

var result2 = _db.Categories
                           .Where(x => x.IsActive == true)
                           .OrderBy(x => x.Position)
                           .Select(x => x.Type)
                           .Distinct()
                           .Where(x => x.IsActive == true && x.AreaId== areaId)
                           .OrderBy(x => x.Position)
                           .ToList();

它运行时没有错误,但没有给出正确的结果,也没有订购子集合

其他不成功的方法:

var result = _db.Types
                           .Where(t => t.IsActive == true)
                           .Where(t => t.SiteId == siteId)
                           .Include(t => t.Categories
                                              .Where(c=>c.IsActive == true)
                                              .OrderBy(c=>c.Position))
                           .OrderBy(t => t.Position)
                           .ToList();

我缺少什么?

感谢。

修改 最终解决方案:

public class TypeDTO
    {
        public Type Type { get; set; }
        public IEnumerable<Category> Categories { get; set; }
    }

var result = _db.Types
                .Where(x => x.IsActive == true)
                .Where(x => x.AreaId== areaId)
                .OrderBy(x=>x.Position)
                .Select(x => new TypeDTO()
                {
                   Type = x,
                   Categories =  x.Categories.Where(c => c.IsActive == true).OrderBy(c => c.Position)
                })
                .ToList();

1 个答案:

答案 0 :(得分:2)

您可以使用以下方法..

using (var context = new DbContext())
{
     var activeTypes = context.Types.Where(t => t.IsActive)
                             .Select(t => new 
                             {
                                 Type= t,
                                 ActiveCategories = t.Categories.Where(c => c.IsActive)
                                                                .OrderBy(c => c.Position)
                             }).ToList();


}