In my application I have version list which is string. Json result looks like:
[
"v2.9.0",
"v2.10.0",
"v2.11.0",
"v2.12.0",
"v2.13.0",
"v2.14.0",
"v2.15.0",
"v2.16.0",
"v2.1.0",
"v2.2.0"
]
I am implementing upgrade version functionality. Which is upgrading currentVersion to targetVersion. Logic is for ex: if current version is v2.9.0 it should be definitely upgrade to 2.10.0 (trick part in our collection we have also 2.1.0 )
I have an idea to get data using this approach to getting max and min numbers but how to get logic for next number I have no idea.
var str = ReadLine();
var digitArray = str.Select(x => int.Parse(x.ToString())).ToList();
var min = int.Parse(string.Concat<string>(digitArray.OrderBy(x => x).Select(x =>
x.ToString())));
var max = int.Parse(string.Concat<string>(digitArray.OrderByDescending(x =>
x).Select(x => x.ToString())));
My project class properties:
public string KubesprayCurrentVersion { get; set; }
public string KubesprayTargetVersion { get; set; }
My kubespray class properties which I am getting list of versions:
public string Version { get; set; }
Getting list of version:
[HttpGet]
public async Task<IActionResult> GetKubesprays()
{
var result = await _context.Kubesprays.Select(x => x.Version).ToListAsync();
return Ok(result);
}
I need upgrade my project kubeSprayTargetVersion using list of kubespray version. For saving I am using approach:
[HttpPost("createproject")]
public async Task<IActionResult> CreateProject(ProjectForCreationDto
projectForCreationDto)
{
var name = projectForCreationDto.Name.ToLower().Trim();
var currentVerison = projectForCreationDto.KubesprayCurrentVersion.Trim();
var targetVersion = string.Copy(currentVerison);
if (await _repo.ProjectExists(name))
return BadRequest("Project name already exists");
var projectToCreate = new Project { Name = name, KubesprayCurrentVersion =
currentVerison, KubesprayTargetVersion = targetVersion};
await _repo.CreateProject(projectToCreate);
return StatusCode(201);
}
I need upgrade kubeSprayTargetVersion to next one actually (in my case it is unsorted list first of all may be I need sort) (currently it is same current and target )
Something like current v2.9.0 => v2.10.0
Thanks in advance.
When working with versions, why not use Version
class?
string[] data = new string[] {
"v2.9.0", "v2.10.0", "v2.11.0", "v2.12.0",
"v2.13.0", "v2.14.0", "v2.15.0", "v2.16.0",
"v2.1.0", "v2.2.0"
};
string[] sorted = data
.OrderBy(item => Version.Parse(Regex.Match(item, @"[0-9]+(?:\.[0-9]+)+").Value))
.ToArray();
Console.Write(string.Join(Environment.NewLine, sorted));
Outcome:
v2.1.0
v2.2.0
v2.9.0
v2.10.0
v2.11.0
v2.12.0
v2.13.0
v2.14.0
v2.15.0
v2.16.0
Having the array sorted , you can easily find out neighbours ( "v2.9.0"
is followed by "v2.10.0"
etc.):
string currentVersion = "v2.9.0";
string nextVersion = sorted[Array.IndexOf(sorted, currentVersion) + 1];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.