linq-> SQL顺序降序不起作用 [英] linq->SQL orderby descending not working

查看:48
本文介绍了linq-> SQL顺序降序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var Customer = (from c in DNAContextSQL.Customers
                                where c.LastName != ""
                                orderby c.PKID_Customer descending
                                select new
                                {
                                    c.PKID_Customer,
                                    c.OrganizationName,
                                    c.FirstName,
                                    c.LastName,
                                    c.Phone,
                                    c.Extension
                                }).Distinct().ToList();

我知道这是基本的.我找不到任何不起作用的理由.发送到SQL Profiler的查询似乎没有 order by 子句.

I know this is basic. I can't find any good reason why it's not working though. The queries sent to SQL Profiler don't seem to have an order by clause in them.

有什么想法吗?

我可以将其与 .OrderByDescending(...)一起使用,但想知道这种疯狂背后的原因.

I can get it to work with .OrderByDescending(...) but would like to know the reason behind this madness.

推荐答案

distinc可能通过尝试在distinct()之后调用orderBy来弄乱订单.

The distinct is probably messing up the order by try calling the orderBy after the distinct()

var Customer = (from c in DNAContextSQL.Customers
                where c.LastName != ""
                select new
                   {
                      c.PKID_Customer,
                      c.OrganizationName,
                      c.FirstName,
                      c.LastName,
                      c.Phone,
                      c.Extension
                    }
                ).Distinct().OrderByDescending(c=>c.PKID_Customer).ToList();

之所以发生这种情况,是因为您首先选择了一组由PKID_Customer排序的行(在您调用distinct()方法之前,这些行是有序的),然后,Distinct()方法将它们重新排列为一个新的独特无序记录集.

This is happening because you first select a set of rows that are ordered by the PKID_Customer (and they are ordered until you call the distinct() method), and after that the Distinct() method rearranges them into a new distinct unordered set of records.

这篇关于linq-> SQL顺序降序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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