我有一个IQueryable扩展方法,我想在2个地方使用:
此查询定义为:
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和导航属性上使用它?
提前致谢