我有这段代码:
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."
答案 0 :(得分:1)
似乎fileID
可能为空。如果使用??
运算符为null,则可以轻松将其转换为空数组。此外,除非您确实需要按索引访问项目,否则.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();