Skip to content

OrderBy-Take-Skip combination gives incorrect result #16669

@smitpatel

Description

@smitpatel

OrderBy followed by Take(2).Skip(1) gives empty list instead of list of size 1.
Repro code:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {

            var cs = 
                new List<Customer>
                {
                    new Customer { CustomerID = "ANATR" },
                    new Customer { CustomerID = "ANTON" },
                    new Customer { CustomerID = "AROUT" },
                    new Customer { CustomerID = "BERGS" },
                    new Customer { CustomerID = "BONAP" },
                    new Customer { CustomerID = "ALFKI" },
                };

            // Only Skip
            Console.WriteLine(cs.Count());
            Console.WriteLine(cs.Skip(1).Count());

            // OrderBy & Skip
            Console.WriteLine(cs.OrderBy(c => c.CustomerID).Count());
            Console.WriteLine(cs.OrderBy(c => c.CustomerID).Skip(1).Count());

            // Take & Skip
            Console.WriteLine(cs.Take(2).Count());
            Console.WriteLine(cs.Take(2).Skip(1).Count());

            // OrderBy, Take & Skip             
            Console.WriteLine(cs.OrderBy(c => c.CustomerID).Take(2).Count());
            Console.WriteLine(cs.OrderBy(c => c.CustomerID).Take(2).Skip(1).Count());

            Console.WriteLine("Hello World!");
        }
    }

    public class Customer {
        public string CustomerID {get; set;}
    }
}

project.json

{
    "version": "1.0.0-*",
    "compilationOptions": {
        "emitEntryPoint": true
    },

    "dependencies": {
        "NETStandard.Library": "1.0.0-rc2-23811"
    },

    "frameworks": {
        "dnxcore50": { }
    }
}

Console Output:

D:\issue> dotnet run
Project issue (DNXCore,Version=v5.0) was previously compiled. Skipping compilation.
6
5
6
5
2
1
2
0 // Should have been 1
Hello World!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions