我有以下代码:
public IList<Folder> GetArchivedFolders(int userId)
{
return _db.Folders.Where(f => f.IsArchived).Where(s =>
IsTopLevelArchivedFolder(s.FolderId)
).ToList();
}
private bool IsTopLevelArchivedFolder(int folderId)
{
var folder = GetFolder(folderId);
if (folder.ParentFolderIdWhileArchived > 0)
{
if (folder.ParentFolderId != null && !GetFolder((int) folder.ParentFolderId).IsArchived)
{
return true;
}
}
return false;
}
这不起作用,因为似乎 EF 无法运行我的代码。
我试过这个:
public System.Linq.Expressions.Expression<Func<Folder, bool>> IsTopLevelArchivedFolder()
{
var folder = this;
if (folder.ParentFolderIdWhileArchived > 0)
{
if (folder.ParentFolderId != null && !GetFolder((int) folder.ParentFolderId).IsArchived)
{
return true;
}
}
return false;
}
但this
正在解析为容器类,而不是Folder
对象。有什么方法可以在这个查询中运行我的代码吗?
更新:
以下代码:
public IList<Folder> GetArchivedFolders(int userId)
{
return _db.Folders.
Where(f => f.IsArchived).
Where(s => IsTopLevelArchivedFolder(s)).
ToList();
}
public bool IsTopLevelArchivedFolder(Folder folder)
{
return (folder.ParentFolderIdWhileArchived > 0) &&
(folder.ParentFolderId != null && !folder.IsArchived);
}
结果是:
LINQ to Entities 无法识别方法 'Boolean IsTopLevelArchivedFolder(Nogginator.Repository.Models.Folder)' 方法,并且此方法无法转换为存储表达式。