我正在尝试从数据库中获取业务模型实体,然后对其进行迭代以搜索字符串。但是,在我的本地主机中,此操作大约需要7 to 9 seconds for 500 objects
.
public List<FileFolder> SearchFolders(int id, string searchString)
{
/* Placeholder that references folders that passes search criteria */
List<FileFolder> addedFolders = new List<FileFolder>();
/* Get the folder with specified id, also include Folders
* that this folder has. No problem here works perfectly fine
* and fast */
FileFolder folder = dbSet.Include("Folders").
.Where(q => q.FileFolderID == id)
.ToList().FirstOrDefault();
/* This takes too much time as I mention, up to 9 seconds
* for 500 Folders */
foreach (FileFolder f in folder.Folders)
{
if (f.Name.Contains(searchString))
{
addedFolders.Add(f);
}
}
return addedFolders;
}
我正在将获取的数据转换为 List 以便我可以安全地说所有数据现在都在内存中?因此,在访问文件夹时,foreach 循环不应再进行任何 db 调用。我也检查了.Include
方法,它工作正常。
这种缓慢迭代的原因可能是什么?
谢谢。