我有这个函数,您传递一个路径到包含文件夹名称的目录,例如UPDATE_20162406_x.x。x(版本)文件将始终具有相同的开头,但日期和版本将不同,我现在需要的是传递一个路径到UPDATE_someDATE_x.x。x到一个函数,检查是否有任何其他文件夹具有相同的版本,这是我需要更改的查询:
public string NewestUpdate(string Directoria)
{
string caminho = "";
Version version = null;
var lastVersion = new DirectoryInfo(Directoria).EnumerateDirectories()
.Where(d => d.Name.ToLower().StartsWith("update_"))
.Select(d => new { Directory = d, Token = d.Name.Split('_') })
.Where(x => x.Token.Length == 3 && Version.TryParse(x.Token[2], out version))
.Select(x => new { x.Directory, Date = x.Token[1], Version = version })
.OrderByDescending(x => x.Version)
.FirstOrDefault();
if (lastVersion != null)
{
caminho = lastVersion.Directory.FullName.ToString();
Global.LatestVersion = lastVersion.Version.ToString();
}
return caminho;
}
我不知道linq几乎任何东西,我不能写适当的查询来做到这一点。任何帮助都是感激的。谢谢你
应该可以:
public static bool NewestUpdate(string Directoria, Version queryVersion)
{
Version version = null;
var foundFolder = (from d in new DirectoryInfo(Directoria).EnumerateDirectories()
where d.Name.ToLower().StartsWith("update_")
let token = d.Name.Split('_')
let vers = token.Length == 3 && Version.TryParse(token[2], out version) ? version : null
where vers == queryVersion
select d).FirstOrDefault();
string directory = foundFolder?.FullName;
return foundFolder != null;
}