【发布时间】:2014-01-28 13:31:02
【问题描述】:
我们正在进行软件构建并将它们存储在预发布文件夹中的文件夹中,格式为四位数 Major.Minor.Build.rev (0.0.0.0)。
- 我想知道是否有办法在 LINQ 查询中使用版本类?
- 使用 LINQ 查询会有什么好处,还是应该坚持使用非 LINQ 查询代码?
这是我目前拥有的代码:
static void Main(string[] args)
{
string latest = GetLatestBuildVersion(@"\\path\to\network\folder");
latest = "";
}
static string GetLatestBuildVersion(string sPath)
{
Version latestVersion = new Version();
string[] dirs = Directory.GetDirectories(sPath);
foreach (string dir in dirs)
{
string subfolderNameOnly = Path.GetFileName(dir);
try
{
Version subfolderVersion = new Version(subfolderNameOnly);
if (latestVersion.CompareTo(subfolderVersion) < 0)
{
latestVersion = subfolderVersion;
}
}
catch (Exception ex)
{
// Just continue
}
}
return latestVersion.ToString();
}
【问题讨论】:
-
如果您已经有了可以工作的代码,那么您似乎应该坚持下去。任何获得的优势都非常小。
-
它确实有效。我希望用更少的代码来做到这一点。我有一些代码可以查询到某个点,但是我卡在 Path.GetFileName 和 subfolderversion 的版本类的第二个实例上。
标签: c# linq linq-to-objects