All, I have written some code to get the latest files from a directory, the files I am looking for contain the string sProcScript = sProcScript and in the selected directory I might have several versions:sProcScriptBigGuns1001.sqlsProcScriptBigGuns1007.sqlsProcScriptEdAggVal1006.sqlsProcScriptEdAggVal1007.sql...and I want to pick up the latest version only (the version number being the number after the file name i.e. 1007 etc.). So in the above this would be sProcScriptBigGuns1007.sql and sProcScriptEdAggVal1007.sql. To do this I wrote the following snippet (as part of a larger method), where strPath is the path to the relevant directoryDirectoryInfo dirInfo = new DirectoryInfo(strPath);List<FileInfo> listFileInfo = new List<FileInfo>();listFileInfo = dirInfo.EnumerateFiles().ToList<FileInfo>();// Loop in to locate special processes.List<FileInfo> specialProcList = new List<FileInfo>();foreach (FileInfo fi in listFileInfo)    if (Path.GetFileNameWithoutExtension(fi.FullName).Contains(sProcScript))        specialProcList.Add(fi);// Get distinct.List<string> nameList = new List<string>();foreach (FileInfo fi in specialProcList){    string strsProc =         Path.GetFileNameWithoutExtension(Regex.Replace(fi.Name, @[\d], String.Empty));    if (!nameList.Contains(strsProc))        nameList.Add(strsProc);}List<FileInfo> tmpList;List<string> tmpNameList = new List<string>();tmpList = specialProcList.OrderByDescending(f => f.Name).ToList();foreach (string name in nameList){    foreach (FileInfo fi in tmpList)    {        if (fi.Name.Contains(name))        {            tmpNameList.Add(fi.Name);            listFilesFound.Add(fi);            break;        }    }}which works great. However, I think there will be a much better and neater way of doing this using LINQ but I am new to LINQ and can't seem to get started with it (the attempts I have made are poor enough to not want to post them here!). Can someone demonstate a better way of doing the above operation using LINQ?Thanks for your time.
Alternative way to extract the most recent file using LINQ
c#;linq
Another one (assuming the filename is always SomeCharactersSomeNumbers.extension):void Main(){    var sProcScript = sProcScript;    var fileNames = new [] {sProcScriptBigGuns1001.sql,                             sProcScriptBigGuns1007.sql,                            sProcScriptEdAggVal1006.sql,                            sProcScriptEdAggVal1007.sql};    var grouped = from f in fileNames                  where f.Contains(sProcScript)                  let m = new {FileName = f,                                Group = new string(f.TakeWhile(c=>!Char.IsDigit(c)).ToArray()),                               Version = Int32.Parse(new string(f.Where(c=>Char.IsDigit(c)).ToArray()))}                  group m by m.Group;    var files = grouped.Select(g => g.OrderByDescending(f => f.Version).First().FileName);                        foreach (var file in files)        Console.WriteLine(file);}