比较int时Linq to Entities错误?到a值

本文关键字:错误 Entities int Linq to 比较 | 更新日期: 2023-09-27 18:21:18

我正在尝试编写一个遍历树的递归函数。我的实体"Folder"有两个属性ParentFolderID和FolderID。如果文件夹处于根级别,则其ParentFolderID为null。我创建了一个递归函数,该函数接受一个参数ID并添加返回与ID具有相同ParentFolderID的所有文件夹实体,但我收到错误"无法创建类型为"System.Object"的常数值。此上下文中只支持基元类型或枚举类型。"。

这是我的功能:

    private List<Folder> GetFolderChildren(int? folderId)
    {
        var childFolders = new List<Folder>();
        //  finalList is List<> that is actually returned
        var finalList = new List<Folder>();
        //  Your looking at the root level folder
        if (folderId == null)
        {
            childFolders = DocumentEntities.Folders.Where(x => x.ParentFolderID.Equals(folderId)).ToList();
        }
        else
        {
            childFolders = DocumentEntities.Folders.Where(x => x.ParentFolderID == folderId).ToList();
        }
        //  If there are no childFolders, then you've hit the bottom of this branch.  Return this folder.
        if (!childFolders.Any())
        {
            finalList.Add(DocumentEntities.Folders.SingleOrDefault(x => x.FolderID == folderId));
        }
        else
        {
            foreach (var childFolder in childFolders)
            {
                finalList.AddRange(GetFolderChildren(childFolder.FolderID, pageName));
            }
            //  Now that you've gone through this folders children, add this folder
            finalList.Add(DocumentEntities.Folders.SingleOrDefault(x => x.FolderID == folderId));
        }
        return finalList;
    }

当folderID为null或为int时,它永远不会到达foreach。有什么想法吗?

比较int时Linq to Entities错误?到a值

如果x.ParentFolderIDnull,您将无法在Equals方法中获得访问权限,因为它为null。尝试将整个值与参数进行比较,因为它具有相同的类型,对于示例

childFolders = Entities.Folders.Where(x => x.ParentFolderID == folderId).ToList();