LINQ重寫博客垃圾圖片回收算法
更新時間:2012年02月24日 11:26:17 作者:
本人博客后臺管理模塊有個功能,可以掃描圖片上傳文件夾下所有未被引用的博客
思路很簡單,從所有Blog Model中解析出所有文章使用的圖片文件名,排除站外引用,放入一個List<string> usedPicList。再遍歷圖片上傳文件夾,把所有圖片文件的結(jié)果加入FileInfo[] fiAllPicList。然后比較usedPicList和fiAllPicList,找出所有fiAllPicList中有,而usedPicList中木有的圖片,就是未被任何文章引用的垃圾圖片了。
原先這個比較算法是用傳統(tǒng)方法寫的,很蛋疼,用了兩重循環(huán),一個標志位才解決問題:
List<FileInfo> garbagePicList = new List<FileInfo>();
for (int k = 0; k < fiAllPicList.Length; k++)
{
bool found = false;
for (int l = 0; l < usedPicList.Count; l++)
{
if (fiAllPicList[k].Name == usedPicList[l].ToString())
{
found = true;
}
}
if (!found)
{
garbagePicList.Add(fiAllPicList[k]);
}
}
今天用LINQ重寫了一下:
List<FileInfo> garbagePicList = new List<FileInfo>();
var query = from pic in fiAllPicList
where !usedPicList.Contains(pic.Name)
select pic;
garbagePicList = query.ToList();
清晰明了
原先這個比較算法是用傳統(tǒng)方法寫的,很蛋疼,用了兩重循環(huán),一個標志位才解決問題:
復制代碼 代碼如下:
List<FileInfo> garbagePicList = new List<FileInfo>();
for (int k = 0; k < fiAllPicList.Length; k++)
{
bool found = false;
for (int l = 0; l < usedPicList.Count; l++)
{
if (fiAllPicList[k].Name == usedPicList[l].ToString())
{
found = true;
}
}
if (!found)
{
garbagePicList.Add(fiAllPicList[k]);
}
}
今天用LINQ重寫了一下:
復制代碼 代碼如下:
List<FileInfo> garbagePicList = new List<FileInfo>();
var query = from pic in fiAllPicList
where !usedPicList.Contains(pic.Name)
select pic;
garbagePicList = query.ToList();
清晰明了
相關文章
VS2010/VS2013項目創(chuàng)建 ADO.NET連接mysql/sql server詳細步驟
這篇文章主要介紹了VS2010/VS2013項目創(chuàng)建,及ADO.NET連接mysql/sql server詳細步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
點擊圖片,AJAX刪除后臺圖片文件的實現(xiàn)代碼(asp.net)
點擊頁面上的圖片,用jQuery的AJAX來刪除后臺真實的文件。2010-11-11
ASP與ASP.NET互通COOKIES的一點經(jīng)驗
ASP與ASP.NET互通COOKIES的一點經(jīng)驗...2006-09-09
.NET中RDLC循環(huán)處理數(shù)據(jù)的應用分析
本篇文章介紹了,.NET中RDLC循環(huán)處理數(shù)據(jù)的應用分析。需要的朋友參考下2013-05-05
asp.net錯誤處理Application_Error事件示例
Application_Error事件與Page_Error事件相類似,可使用他捕獲發(fā)生在應用程序中的錯誤。由于事件發(fā)生在整個應用程序范圍內(nèi),因此您可記錄應用程序的錯誤信息或處理其他可能發(fā)生的應用程序級別的錯誤2014-01-01

