2

我有这段代码:

var files = (from f in _fileRep.GetFiles()
              where fileID.Contains(f.FileID)
              select f.FileName).ToList();

var destFiles = (from f in _fileRep.GetFiles()
                    where f.FolderID == destFolderID 
                     select f.FileName).ToList();

bool valid = destFiles.Any(x => files.Contains(x));

fileID 是 int [],我正在尝试验证在数据库中移动文件时 FolderID 与 destFolderID 匹配的文件名列表是否存在。

我越来越:

"Unable to create a null constant value of type 'System.Int32[]'. 
Only entity types, enumeration types or primitive types are 
supported in this context."
4

1 回答 1

1

似乎这可能fileID是空的。如果它为空,则可以使用??运算符轻松将其转换为空数组。此外,除非您真的需要按索引访问项目,否则.ToList可能没有必要。

你可以试试这个:

fileID = fileID ?? new int[0];
var files = _fileRep.GetFiles().Where(f => fileID.Contains(f.FileID));
var destFiles = _fileRep.GetFiles().Where(f => f.FolderID == destFolderID);
bool valid = files.Join(destFiles, f => f.FileName, f => f.FileName).Any();

甚至这样:

fileID = fileID ?? new int[0];
bool valid = 
    (from f in _fileRep.GetFiles()
     join g in _fileRep.GetFiles() on f.FileName equals g.FileName
     where fileID.Contains(f.FileID) && g.FolderID == destFolderID
     select f).Any();
于 2013-06-03T08:45:22.827 回答