1

I want to store and sort Unicode characters using Asp.net MVC and Entity Framework. So what is the best way to do this. I crate Model, Student Model:

public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public DateTime EnrollmentDate { get; set; }
    }

In the StudentController at index action:

public ActionResult Index(string sortOrder)
{
   ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
   ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
   var students = from s in db.Students
                  select s;
   switch (sortOrder)
   {
      case "name_desc":
         students = students.OrderByDescending(s => s.LastName);
         break;
      case "Date":
         students = students.OrderBy(s => s.EnrollmentDate);
         break;
      case "date_desc":
         students = students.OrderByDescending(s => s.EnrollmentDate);
         break;
      default:
         students = students.OrderBy(s => s.LastName);
         break;
   }
   return View(students.ToList());
}

I use "students = students.OrderByDescending(s => s.LastName);" to sort student name. It works with English ,but it doesn't work Unicode like Khmer characters

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • 2
    What have you tried so far? Support for Unicode is generally built in into .NET and (ASP.NET MVC as well), what difficulties do you have? – Petr Abdulin Apr 21 '14 at 02:04
  • I use "students = students.OrderByDescending(s => s.LastName);" to sort student name. It works with English ,but it doesn't work Unicode like Khmer characters. – user3555183 Apr 21 '14 at 02:13
  • It's generally better to edit your question, than add related information in comments. So, show us what `students` contain, what the results are, and what you expect. It's really worth spend some time and write a good question if you want quick and good answer: https://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Petr Abdulin Apr 21 '14 at 02:23
  • Is your application culture set to one that supports Khmer characters? – NeddySpaghetti Apr 21 '14 at 03:53

2 Answers2

2

If you have set your current culture to Khmer, you should be able to specify to use the current culture explicitly in the OrderByDescending method

    OrderByDescending (s => s, StringComparer.CurrentCulture)

Alternatively you can create a Khmer culture and pass it in to the StringComparer

var culture = new CultureInfo("km-KH");
var result = students.OrderBy(s => s, StringComparer.Create(culture, false));

See this answer from Jon Skeet: How do I get LINQ to order according to culture?

Community
  • 1
  • 1
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
0

Unicode is supported in Entity Framework by default, but for your problem in sorting it refers to your database collation or (column collation if you defined that), so make sure your database collation is set in to support Khemr chars.

Reza
  • 18,865
  • 13
  • 88
  • 163