0

I have a model called ProjectVersion which has the following properties:

    int Major
    int Minor
    int Patch
    int? Revision
    string PreRelease
    string MetaData

Now I am trying to order the versions in decreasing order according to the Semantic Versioning 2.0.0 specification where precedence is Major > Minor > Patch > PreRelease

I'm also using Entity framework and Linq to get my data, here is the code I'm currently using:

Note: all less than'<' and greater than '>' signs in the code below are replaced with pipes '|' due to my inexperience with stackoverblow.

 public List|ProjectVersionDataContract| GetProjectVersions(int projectId)
        {
            // TODO: Evaluate Versioning system and order list according to it's specification.
            var versions = new List|ProjectVersionDataContract|();
            using (var context = new Entities())
            {
                var vers = context.ProjectVersions.Where(v => v.ProjectId == projectId)
                    .OrderBy(v => v.MajorVersion)
                    .ThenBy(v => v.MinorVersion)
                    .ThenBy(v => v.PatchVersion)
                    .ThenBy(v => v.RevisionVersion)
                    .ThenBy(v => v.PreReleaseVersion).ToList();

                // This uses the AutoMapper package to map the Data Object to the Data Contract
                versions.AddRange(vers.Select(Mapper.Map|ProjectVersion, ProjectVersionDataContract|));
            }

            return versions;
        }

Now this get's my data but it returns it with the lowest (oldest) version first, which is opposite of what I want. I've even tried changing the .OrderBy() to OrderByDescending() with no difference. This is a WCF service if that makes any difference.

7
  • could you .Reverse() it Commented Aug 7, 2013 at 4:00
  • didn't realize that was a function, I'll try it and let you know. Commented Aug 7, 2013 at 4:07
  • updated the query to var vers = context.ProjectVersions.Where(v => v.ProjectId == projectId) .OrderBy(v => v.MajorVersion) .ThenBy(v => v.MinorVersion) .ThenBy(v => v.PatchVersion) .ThenBy(v => v.RevisionVersion) .ThenBy(v => v.PreReleaseVersion).Reverse().ToList(); got the following: Commented Aug 7, 2013 at 4:09
  • LINQ to Entities does not recognize the method 'System.Linq.IQueryable'1[Data.Model.ProjectVersion] Reverse[ProjectVersion](System.Linq.IQueryable'1[Data.Model.ProjectVersion])' method, and this method cannot be translated into a store expression. Commented Aug 7, 2013 at 4:09
  • 1
    Reverse would be after ToList Commented Aug 7, 2013 at 4:10

1 Answer 1

0

try

var vers = context.ProjectVersions.Where(v => v.ProjectId == projectId)
                    .OrderByDescending(v => v.MajorVersion)
                    .ThenByDescending(v => v.MinorVersion)
                    .ThenByDescending(v => v.PatchVersion)
                    .ThenByDescending(v => v.RevisionVersion)
                    .ThenByDescending(v => v.PreReleaseVersion).ToList();

Or you can do as below

var normal = GetProjectVersions(idval);
var finalResult =normal.Reverse(); 
Sign up to request clarification or add additional context in comments.

1 Comment

I did the last one. worked great. Thank you for the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.