LINQ rewrites the blog spam image recovery algorithm

  • 2020-05-17 05:07:38
  • OfStack

The idea is simple: parse out the image file names used in all articles from all Blog Model, exclude off-site references, and put in one List < string > usedPicList. Go through the image upload folder again and add the results of all the image files to FileInfo[] fiAllPicList. Then compare usedPicList and fiAllPicList, and find out that all the pictures in fiAllPicList and none in usedPicList are junk pictures that have not been quoted in any article.
Originally, this comparison algorithm was written in the traditional way, which was very painful. It took two cycles and one flag bit to solve the problem:
 
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]); 
} 
} 

Today, I rewrote 1 with LINQ:
 
List<FileInfo> garbagePicList = new List<FileInfo>(); 
var query = from pic in fiAllPicList 
where !usedPicList.Contains(pic.Name) 
select pic; 
garbagePicList = query.ToList(); 

clear

Related articles: