Ordering individual values ​​using LINQ

Possible duplicate:
How to get a separate ordered list of names from a DataTable using LINQ?

This is my first question. I get a list of different values ​​for the dropdown from my database as follows:

var plocQ = (from m in db.SERVICE_NRS
             orderby m.PLOC
             select new { PlocID = m.PLOC, value = m.PLOC }).Distinct();

Order seems to have no effect, I have to do this:

var plocQ = (from m in db.SERVICE_NRS
             select new { PlocID = m.PLOC, value = m.PLOC }).Distinct();

plocQ = from s in plocQ
        orderby s.PlocID
        select s;

I am wondering if this is related to LINQ or the database? I am a little new to LINQ and have written too much SQL before. Any ideas?

+4
source share
1 answer

This is because you change what is in your projection after sorting the initial results. Distinctdoes not guarantee the preservation of the order.

, , ! , .

, :

var plocQ = (from m in db.SERVICE_NRS
             select new { PlocID = m.PLOC, value = m.PLOC })
            .Distinct()
            .OrderBy(s => s.PlocID);
+6

All Articles