Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq orderby culture (danish, æøå)

I'm having a problem with my orderby linq expression. It generates the output in a wrong order. I'm from Denmark and creating a danish website, therefor the order has to be exact ofc.

Here is my query:

var model = (from w in db.News
                orderby w.Title
                select w).ToList();

The output is:

1, 123
2, æøå
3, hallo
4, know

The correct order should be like this:

1, 123
2, hallo
3, know
4, æøå

How do I correct this?

like image 690
aventic Avatar asked Apr 13 '13 23:04

aventic


1 Answers

You can pass string comparer to OrderBy method if you will use fluent Linq syntax:

var model = db.News.OrderBy(w => w.Title, StringComparer.InvariantCulture)
              .ToList();

BTW you can create string comparer specific to your culture with StringComparer.Create method:

StringComparer.Create(new CultureInfo("da-DK"), true)
like image 80
Sergey Berezovskiy Avatar answered Oct 02 '22 15:10

Sergey Berezovskiy