Rohit Raghuvansi
Rohit Raghuvansi

Reputation: 2864

how to find last version no from a list<> in C#

I have a entity class RenderingTemplates. Inside this i have a property List which holds all versions of rendering template. RenderingTemplateVersion has a property VersionName which stores version name as "Version 1.0".

I am creating a new version and want to find last version no.so that i can append it by 1 and make new VersionName as "Version 2.0".

To accomplish this i have

LatestVersion =  template.RenderingTemplateVersionList.OrderByDescending(e => e.VersionName.Split(new char[] { ' ', '.' })[1]).First()

LatestVersion is a integer. How to convert this to integer.Please help or suggest some other way.

Upvotes: 1

Views: 663

Answers (5)

Thurein
Thurein

Reputation: 2566

Reactive Extension is needed for the following example:

        int versionNo = template.RenderingTemplateVersionList.Select(v => v.VersionName.Split(new char[] { ' ', '.' }, StringSplitOptions.RemoveEmptyEntries).ElementAt(1))
                                                             .Catch(EnumerableEx.Return<string>(int.MinValue.ToString(CultureInfo.InvariantCulture)))
                                                             .Let(vl => 
                                                                        vl.Any(v => v == int.MinValue.ToString(CultureInfo.InvariantCulture)) ? 
                                                                        EnumerableEx.Return<string>(int.MinValue.ToString(CultureInfo.InvariantCulture)) : vl
                                                                 )
                                                             .Select(v => Convert.ToInt32(v))
                                                             .Catch(EnumerableEx.Return<int>(int.MinValue))
                                                             .Let(vl => vl.Any(v => v == int.MinValue)? EnumerableEx.Return<int>(int.MinValue) : vl)
                                                             .OrderByDescending(v => v)
                                                             .DefaultIfEmpty(0)
                                                             .FirstOrDefault();



        if (versionNo == int.MinValue)
        {
            // Error in VersionName Format
        }
        else
        {
            if (versionNo > 0)
            {
                int newVersionNo = versionNo++;
            }
            else
            { 
                // There is no current version available
            }
        }

I know it is a bit complex and overdone as compared to other methods but it is something which can be done using Rx Extension. It would be particularly useful if you want to go by Linq method chains only.

Upvotes: 0

Carra
Carra

Reputation: 17964

I'd advise you to just use the Version class. Then you can just sort your list and take the last item.

Upvotes: 3

simendsjo
simendsjo

Reputation: 4749

var versions = new[] { "Version 2.0", "Version 2.1", "Version 1.5" };
var highest = versions.OrderByDescending(e => new Version(e.Replace("Version", ""))).First();

Upvotes: 0

Sani Huttunen
Sani Huttunen

Reputation: 24385

var latestVersionInteger = Convert.ToInt32(LatestVersion);

or

int latestVersionInteger;
int.TryParse(LatestVersion, latestVersionInteger);

Upvotes: 0

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158309

I usually recommend using TryParse to go from string to int:

int LatestVersion;
if (int.TryParse(template.RenderingTemplateVersionList.OrderByDescending(e => e.VersionName.Split(new char[] { ' ', '.' })[1]).First(), out LatestVersion)
{
    // LatestVersion how has the version number in it
}

Upvotes: 0

Related Questions