zl程序教程

您现在的位置是:首页 >  其他

当前栏目

LINQ重写博客垃圾图片回收算法

博客算法重写 图片 垃圾 回收 LinQ
2023-06-13 09:14:32 时间
思路很简单,从所有BlogModel中解析出所有文章使用的图片文件名,排除站外引用,放入一个List<string>usedPicList。再遍历图片上传文件夹,把所有图片文件的结果加入FileInfo[]fiAllPicList。然后比较usedPicList和fiAllPicList,找出所有fiAllPicList中有,而usedPicList中木有的图片,就是未被任何文章引用的垃圾图片了。
原先这个比较算法是用传统方法写的,很蛋疼,用了两重循环,一个标志位才解决问题:
复制代码代码如下:

List<FileInfo>garbagePicList=newList<FileInfo>();
for(intk=0;k<fiAllPicList.Length;k++)
{
boolfound=false;
for(intl=0;l<usedPicList.Count;l++)
{
if(fiAllPicList[k].Name==usedPicList[l].ToString())
{
found=true;
}
}
if(!found)
{
garbagePicList.Add(fiAllPicList[k]);
}
}

今天用LINQ重写了一下:
复制代码代码如下:

List<FileInfo>garbagePicList=newList<FileInfo>();
varquery=frompicinfiAllPicList
where!usedPicList.Contains(pic.Name)
selectpic;
garbagePicList=query.ToList();

清晰明了