Oxymoron
Oxymoron

Reputation: 1452

Ordering with Linq to SQL c#.NET WCF

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.

Upvotes: 0

Views: 258

Answers (1)

Damith
Damith

Reputation: 63105

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(); 

Upvotes: 0

Related Questions