c# - Removing elements with Except -
below have method supposed delete versions of file except newest. except
call not producing result i'm expecting , after looking @ docs once again can't seem understand why. purpose of debugging i've moved linq query except
out of foreach condition. when debug newest
correctly set recent log file, after next line executes todelete
still contains newest
, why? missing here? relevant code below. method gets called passing file set static part of log name, after has time stamp. both where(x => x.contains(file))
work expect them to.
public static void cleanuplocalcopies(string file) { string[] localfiles = directory.getfiles(".\\"); string newest = files.where(x => x.contains(file)).orderbydescending(x => x).firstordefault(); var todelete = localfiles.where(x => x.contains(file)).except(new string[] { newest }); foreach (string f in todelete) { file.delete(f); } }
in case, can use skip skip first file directly, , avoid except
call:
var todelete = files.where(x => x.contains(file)).orderbydescending(x => x).skip(1);
Comments
Post a Comment