c# - how to efficiently Comparing two lists with 500k objects and strings -
so have main directory sub folders , around 500k images. know alot of theese images not exist in database , want know ones can delete them.
this code have far:
var listofadpicturenames = imagedb.getalladpicturenames(); var listwithfilesfromimagefolder = imagedirsearch(adpicturespath); var result = listwithfilesfromimagefolder.where(p => !listofadpicturenames.any(q => p.filename == q)); var differencelist = result.tolist();
listofadpicturenames of type list<string>
here model im returing imagedirsearch:
public class checknotusedadimagesmodel { public list<imagedirmodel> listwithunusedadimages { get; set; } } public class imagedirmodel { public string filename { get; set; } public string path { get; set; } }
and here recursive method images folder.
private list<imagedirmodel> imagedirsearch(string path) { string adpicturespath = configurationmanager.appsettings["adpicturespath"]; list<imagedirmodel> files = new list<imagedirmodel>(); try { foreach (string f in directory.getfiles(path)) { var model = new imagedirmodel(); model.path = f.tolower(); model.filename = path.getfilename(f.tolower()); files.add(model); } foreach (string d in directory.getdirectories(path)) { files.addrange(imagedirsearch(d)); } } catch (system.exception excpt) { throw new exception(excpt.message); } return files;
}
the problem have row:
var result = listwithfilesfromimagefolder.where(p => !listofadpicturenames.any(q => p.filename == q));
takes on hour complete. want know if there better way check in images folder if there images there doesn't exist in database.
here method image names database layer:
public static list<string> getalladpicturenames() { list<string> listwithalladfilenames = new list<string>(); using (var db = new databaselayer.dbentities()) { listwithalladfilenames = db.ad_pictures.select(b => b.filename.tolower()).tolist(); } if (listwithalladfilenames.count < 1) return new list<string>(); return listwithalladfilenames; }
perhaps except you're looking for. this:
var filesinfoldernotindb = listwithfilesfromimagefolder.select(p => p.filename).except(listofadpicturenames).tolist();
should give files exist in folder not in database.
Comments
Post a Comment