简体   繁体   中英

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

  • Visual Studio 2022 Enterprise 17.4.4
  • .NET 6 Console project

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"

在此处输入图像描述

The question

What am i missing? :)

Thank you very much

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM