c# order by 用法

class OrderbySample1
{
    static void Main()
    {
        // Create a delicious data source.
        string[] fruits = { "cherry", "apple", "blueberry" };

        // Query for ascending sort.
        IEnumerable<string> sortAscendingQuery =
            from fruit in fruits
            orderby fruit //"ascending" is default
            select fruit;

        // Query for descending sort.
        IEnumerable<string> sortDescendingQuery =
            from w in fruits
            orderby w descending
            select w;            

        // Execute the query.
        Console.WriteLine("Ascending:");
        foreach (string s in sortAscendingQuery)
        {
            Console.WriteLine(s);
        }

        // Execute the query.
        Console.WriteLine(Environment.NewLine + "Descending:");
        foreach (string s in sortDescendingQuery)
        {
            Console.WriteLine(s);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
Ascending:
apple
blueberry
cherry

Descending:
cherry
blueberry
apple
*/
class OrderbySample2
{
    // The element type of the data source.
    public class Student
    {
        public string First { get; set; }
        public string Last { get; set; }
        public int ID { get; set; }
    }

    public static List<Student> GetStudents()
    {
        // Use a collection initializer to create the data source. Note that each element
        //  in the list contains an inner sequence of scores.
        List<Student> students = new List<Student>
        {
           new Student {First="Svetlana", Last="Omelchenko", ID=111},
           new Student {First="Claire", Last="O‘Donnell", ID=112},
           new Student {First="Sven", Last="Mortensen", ID=113},
           new Student {First="Cesar", Last="Garcia", ID=114},
           new Student {First="Debra", Last="Garcia", ID=115}
        };

        return students;

    }
    static void Main(string[] args)
    {
        // Create the data source.
        List<Student> students = GetStudents();

        // Create the query.
        IEnumerable<Student> sortedStudents =
            from student in students
            orderby student.Last ascending, student.First ascending
            select student;

        // Execute the query.
        Console.WriteLine("sortedStudents:");
        foreach (Student student in sortedStudents)
            Console.WriteLine(student.Last + " " + student.First);           

        // Now create groups and sort the groups. The query first sorts the names
        // of all students so that they will be in alphabetical order after they are
        // grouped. The second orderby sorts the group keys in alpha order.
        var sortedGroups =
            from student in students
            orderby student.Last, student.First
            group student by student.Last[0] into newGroup
            orderby newGroup.Key
            select newGroup;

        // Execute the query.
        Console.WriteLine(Environment.NewLine + "sortedGroups:");
        foreach (var studentGroup in sortedGroups)
        {
            Console.WriteLine(studentGroup.Key);
            foreach (var student in studentGroup)
            {
                Console.WriteLine("   {0}, {1}", student.Last, student.First);
            }
        }

        // Keep the console window open in debug mode
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
sortedStudents:
Garcia Cesar
Garcia Debra
Mortensen Sven
O‘Donnell Claire
Omelchenko Svetlana

sortedGroups:
G
   Garcia, Cesar
   Garcia, Debra
M
   Mortensen, Sven
O
   O‘Donnell, Claire
   Omelchenko, Svetlana
*/
时间: 2024-10-07 06:07:04

c# order by 用法的相关文章

Oracle中的 row_number() over (partition by order by ) 用法

oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_name;; 这句话的意思是把表中的数值按照colx 分组,每一组内部按照coly排序,同时 row_number()返回排序之后该记录在改组内部的序号. 比如我们知道有emp表如下: SQL> SELECT * FROM SCOTT.EMP; EMPNO ENAME JOB MGR HIREDATE

Pig order by用法举例

sorted = order data by $0; 数值类型按照数值大小比较 chararray类型按照字符的字典顺序比较 bytearray按照字节的字典顺序比较 复杂类型(map.tuple.bag)不能比较 null是最小的 触发reduce阶段 sorted = order data by $0; sorted = order data by $0 desc, $1 asc; order by是全局排序,有reduce阶段,有可能产生数据倾斜,为解决这个问题,Pig在shuffle过程

order by用法

此文来源于CSDN中zxcvg的博文 1.ORDER BY 中关于NULL的处理 缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前. 当然,你也可以使用nulls first 或者nulls last 语法来控制NULL的位置.Nulls first和nulls last是Oracle Order by支持的语法如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc 还是 d

row_number() over (partition by....order by...)用法 分组排序

转载来源:http://www.cnblogs.com/Kazaf/archive/2011/06/30/2094015.html row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) SELECT G.*, ROW_NUMBER() OVER(PARTITION BY a ,b ORDER BY c DESC) ROWN fr

Mysql多字段order by用法

今天运维提出数据展示增加处理标识排序功能,看了代码发现原来已经order by了,是以id倒序排的,现需要同时对两个字段进行排序. 加了个参数试了一下,结果报错了.... 后来发现少了个逗号... mysql中,我们可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升序排列. order by 后可加2个字段,用英文逗号隔开, 如A用升序, B降序,SQL该这样写,order by A ASC,  B DESC;也可以这样写:order by A, B

oracle查询中over(partition by ...order by ...)用法

例: rank() over(partition by deptno order by sal desc) rank 解释: deptno -- 部门, sal -- 工资 over:  在什么条件之上.partition by e.deptno:  按部门编号划分(分区).order by e.sal desc:  按工资从高到低排序(使用rank()/dense_rank() 时,必须要带order by否则非法)rank():  分级 整个语句的意思就是:在按部门划分的基础上,按工资从高到

apache config directive &ndash; order, allow, deny

在对apache进行配置的时候,常看到oerder, allow, deny.现在就简单回顾一下其用法. 对于每个对于资源的请求,服务器可以配置是否允许这个请求通过.在配置当中,使用的是允许与不允许的配置方案. 最终这个请求是否允许,可能是多个配置的综合作用的结果. 也就是allow, deny. 那么,如果没有allow, 也没有deny,默认是allow,还是deny呢,这就是order的用法. order allow, deny,默认deny. order deny, allow,默认al

ruby where用法

用法1 Subject.where("position=?","2").order("name") 用法2 与find方法不同的是,where方法返回的结果不是数组而是ActiveRelation,这意味着我们可以基于当前的查询结果继续设置条件进行查询. Subject.where(:position =>"2").class => ActiveRecord::Relation 用法3 通过to_sql方法我们能看

sql中order by和group by的区别

order by 和 group by 的区别: 1,order by 从英文里理解就是行的排序方式,默认的为升序. order by 后面必须列出排序的字段名,可以是多个字段名. 2,group by 从英文里理解就是分组.必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段. 3,在使用group by的语句中,只能select用于分类的列(表达式),或聚合函数.where条件用于group by之前,having用于group by 之后对结果进行筛选. 扩展资料: 一.ord