订货与LINQ不同的值 [英] Ordering Distinct values with LINQ

查看:146
本文介绍了订货与LINQ不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
如何得到清晰,下令从一个DataTable使用LINQ名单?

这是我的第一个问题在这里。我得到不同值的列表下拉列表从我的数据库,像这样:

This is my first question here. I am getting a list of distinct values for a drop-down list from my database like so:

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



排序似乎没有任何效果,我必须这样做:

The ordering 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;



我想知道,如果这事做的LINQ或数据库?我是一个有点新的LINQ和之前已经写过多SQL。任何想法?

I am wondering if this has something to do with LINQ or the database? I am a bit new to LINQ and have written too much SQL before. Any ideas?

推荐答案

这是因为你改变什么在你的投影的之后的你整理初始结果。 鲜明不保证该顺序被保留。

It's because you're changing what's in your projection after you sort the initial results. Distinct doesn't guarantee that the order is preserved.

顺便说一下,即使它的没有的工作方式,你仍然不会想!你会从项目的整个列表进行排序,即使其中一些人只是将被抛出。

Incidentally, even if it did work that way, you still wouldn't want to! You'd be sorting through the whole list of items, even though some of them were just going to be thrown out.

如果你只想做这一切在一声明中,你可以这样做:

If you just want to do this all in one statement, you can do this:

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

这篇关于订货与LINQ不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆