可组合实体框架查询导航属性

时间:2014-10-21 04:12:33

标签: c# linq entity-framework entity-framework-6 navigation-properties

我有一个IQueryable扩展方法,我想在2个地方使用:

  1. DbSet
  2. 导航属性
  3. 此查询定义为:

    public static class TrainingModuleQuery
    {
        /// <summary>
        /// Modules have to be exposed to the state or user group, plus be active.
        /// </summary>
        public static IQueryable<TrainingModule> ExposedModules(this IQueryable<TrainingModule> folders, int stateID, int userGroupID)
        {
            return folders.Where(m =>
                (m.UserGroups.Any(ug => ug.UserGroupID == userGroupID) || m.States.Any(s => s.StateID == stateID)) &&
                m.StatusID == (int)Status.Active);
        }
    }
    

    并使用如下:

        public static IQueryable<TrainingFolder> ExposedFolders(int stateID, int userGroupID)
        {
            return ObjectContext.TrainingFolder
                                .Where(f => f.Modules
                                             .AsQueryable()
                                             .ExposedModules(stateID, userGroupID)
                                             .Any());
        }
    

    但是,当我尝试运行此代码时,我得到了异常:

    LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[Data.TrainingModule] ExposedModules(System.Linq.IQueryable`1[Data.TrainingModule], Int32, Int32)' method, and this method cannot be translated into a store expression.
    

    有什么方法可以定义这个查询并在DbSet和导航属性上使用它?

    提前致谢

0 个答案:

没有答案