1

我首先使用 EF4.1 代码,

我正在尝试创建一个查询,该查询返回一个带有急切加载的子集合的集合。两者都必须按PositionIsActive == 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.IEnumerable1[...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();
4

1 回答 1

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


}
于 2011-07-22T09:26:31.740 回答