开发者

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

开发者 https://www.devze.com 2023-01-03 23:22 出处:网络
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 na

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.Plea开发者_运维知识库se help or suggest some other way.


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


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
}


var latestVersionInteger = Convert.ToInt32(LatestVersion);

or

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号