tags:

views:

94

answers:

5

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.

A: 

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
}
Fredrik Mörk
A: 
var latestVersionInteger = Convert.ToInt32(LatestVersion);

or

int latestVersionInteger;
int.TryParse(LatestVersion, latestVersionInteger);
Sani Huttunen
A: 
var versions = new[] { "Version 2.0", "Version 2.1", "Version 1.5" };
var highest = versions.OrderByDescending(e => new Version(e.Replace("Version", ""))).First();
simendsjo
+3  A: 

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

Carra
A: 

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.

Kthurein