Linq orderby culture (Danish, æøå)

I have a problem with my orderby linq expression. It generates the result in the wrong order. I am from Denmark and am creating a Danish website, so the order must be accurate.

Here is my request:

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

Output:

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

The correct order should be like this:

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

How to fix it?

+6
source share
1 answer

You can pass a string mapper to the OrderBy method if you use the free Linq syntax:

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

BTW, you can create a string mapper specific to your culture using the StringComparer.Create method:

 StringComparer.Create(new CultureInfo("da-DK"), true) 
+9
source

All Articles