thepill
thepill

Reputation: 61

LINQ's OrderBy behaves different in Visual Studio's Immediate window than within the code

i see diffrent results when using LINQ's OrderBy function on a list in .NET and Visual Studio's Immediate window:

Info

Code

var l = new List<string>() {
            "a-test.de",
            "a.de"
        };


Console.WriteLine(l.OrderBy(e => e).ToList().First());

Result when running the programm

Output is "a-test.de"

Result when using the immediate window

If i set a debugger after the console output and i run l.OrderBy(e => e).ToList().First() within the immediate window the output is "a.de"

enter image description here

The question

What am i missing? :)

Thank you very much

Upvotes: 2

Views: 137

Answers (1)

Denis Fedak
Denis Fedak

Reputation: 171

I suppose it depends on your current culture

Try to specify comparer for OrderBy

 var l = new List<string>() {
            "a-test.de",
            "a.de"
        };


Console.WriteLine(l.OrderBy(e => e, StringComparer.Ordinal).ToList().First());

It should achieve the same behavior

Upvotes: 2

Related Questions