在C#Entity Framework中使用Repository Pattern进行多个嵌套对象查询

时间:2014-12-31 18:07:09

标签: c# linq entity-framework repository-pattern

我正在尝试在现有数据模型上实现存储库模式。 我有以下所有需要用来获取树结构数据的实体

获取GenericCategory树的列表,其中层次结构中至少有一个产品

GenericCategory // List of GenericCategory with all its descendant nodes
  -Category
    -CategorySection
      -SectionProduct
        -Product // At least one product is 

我的域类

public class GenericCategory{

 public GenericCategory()
        {
            this.Category = new HashSet<Category>();
        }

        [Key]
        public Guid Gct_ID { get; set; }
       // All other properties
}

public class Category{

 public Category()
        {
            this.Category = new HashSet<CategorySection>();
        }

        [Key]
        public Guid Gct_ID { get; set; }
       // All other properties
}

public class CategorySection{

 public CategorySection()
        {
            this.Category = new HashSet<SectionProduct>();
        }

        [Key]
        public Guid Gct_ID { get; set; }
       // All other properties
}


public class SectionProduct{

 public SectionProduct()
        {
            this.Category = new HashSet<Product>();
        }

        [Key]
        public Guid Gct_ID { get; set; }
       // All other properties
}

公共类产品{      公共产品()

        [Key]
        public Guid Gct_ID { get; set; }
       // All other properties
}

GenericCategory

 public class GenericCategory : IGenericCategoryRepository
    {
        Fast8xEntities context = new Fast8xEntities();

        public IQueryable<Cc_Sys_Gct_GenericCategory_S> All
        {
            get { return context.Cc_Sys_Gct_GenericCategory_S; }
        }
      .............
   }

IGenericCategoryRepository

public interface IGenericCategoryRepository : IDisposable
    {
        IQueryable<Cc_Sys_Gct_GenericCategory_S> All { get; }
        IQueryable<Cc_Sys_Gct_GenericCategory_S> AllIncluding(params Expression<Func<Cc_Sys_Gct_GenericCategory_S, object>>[] includeProperties);
        Cc_Sys_Gct_GenericCategory_S Find(System.Guid id);
        void InsertOrUpdate(Cc_Sys_Gct_GenericCategory_S cc_sys_gct_genericcategory_s);
        void Delete(System.Guid id);
        void Save();
    }

业务层呼叫

using (var repository = new DAL.Models.GenericCategoryRepository())
            {

result = repository.All.Where(gc =>
                    gc.Category.Any(c =>
                    c.CategorySection.Any(cs =>
                    cs.SectionProduct.Any(sp =>
                    sp.Product.Any())))).ToList();
           }

错误 指定的类型成员&#39; CategorySection&#39; LINQ to Entities不支持。仅支持初始化程序,实体成员和实体导航属性。

是否可以在LINQ中执行类似的操作

0 个答案:

没有答案