.net5,net6的Linq排序OrderBy结果不对的解决方案

OrderBy方法作为Linq的排序,是经常用的方法。

在.net5之前的版本,用法直接是:

var newList= ulist.OrderBy(x => x.UserName).ToList();

但是自从.net5后,这样就会出现异常,有中文、英文都有的时候,排序就会出现问题。

解决方案:

需要加个参数StringComparer.InvariantCulture,比如:

var newList= ulist.OrderBy(x => x.UserName, StringComparer.InvariantCulture).ToList();

 

 

原因:

https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu

https://learn.microsoft.com/en-us/dotnet/standard/base-types/string-comparison-net-5-plus

 

posted @ 2022-10-14 17:00  沐雪架构师  阅读(302)  评论(0编辑  收藏  举报