Demo(List 排序,Linq查询)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student { Name = "jay", Heigh = 181, BirthTime = new DateTime(1994, 08, 07, 12, 08, 03) };
            Student stu2 = new Student { Name = "cris", Heigh = 170, BirthTime = new DateTime(1992, 12, 07, 16, 01, 45) };
            Student stu3 = new Student { Name = "tmo", Heigh = 165, BirthTime = new DateTime(1990, 06, 25, 13, 22, 06) };
            Student stu4 = new Student { Name = "james", Heigh = 165, BirthTime = new DateTime(1998, 09, 07, 22, 31, 16) };
            Student stu5 = new Student { Name = "krod", Heigh = 165, BirthTime = new DateTime(2000, 08, 29, 18, 08, 06) };
            Student stu6 = new Student { Name = "luver", Heigh = 184, BirthTime = new DateTime(2009, 11, 07, 05, 54, 21) };
            Student stu7 = new Student { Name = "jerry", Heigh = 176, BirthTime = new DateTime(1989, 04, 11, 09, 21, 41) };
            Student stu8 = new Student { Name = "jim", Heigh = 145, BirthTime = new DateTime(1979, 01, 07, 06, 18, 06) };
            Student stu9 = new Student { Name = "harry", Heigh = 164, BirthTime = new DateTime(2006, 02, 22, 08, 08, 14) };
            Student stu10 = new Student { Name = "kobe", Heigh = 165, BirthTime = new DateTime(1992, 03, 07, 07, 18, 16) };

            List<Student> students = new List<Student>();
            students.Add(stu1);
            students.Add(stu2);
            students.Add(stu3);
            students.Add(stu4);
            students.Add(stu5);
            students.Add(stu6);
            students.Add(stu7);
            students.Add(stu8);
            students.Add(stu9);
            students.Add(stu10);


            var studentsHeighsOrder = students.OrderBy(p => p.Heigh).ToList();//按int排序
            var studentsHeighsOrderByDescending = students.OrderByDescending(p => p.Heigh).ToList();//按int反排序
            var studentsNameOrder = students.OrderBy(p => p.Name).ToList();//按string排序,按ASCII码排序
            var studentsBirthTimeOrder = students.OrderBy(p => p.BirthTime).ToList();//按DateTime排序
            var studentsHeighsAndBirthTimeOrder = students.OrderBy(p => p.Heigh).ThenBy(p => p.BirthTime).ToList();//按int排序后再按//按DateTime排序(这种一般用在OrderBy(p => p.Heigh)有并列的情况,如都是165)
            var studentsHeighsAndBirthTimeDescending = students.OrderBy(p => p.Heigh).ThenByDescending(p => p.BirthTime).ToList();//按int排序后再按//按DateTime反排序

            //  Method1 Query
            var studentWhere = students.Where(p => p.Heigh > 20 && p.Heigh < 80 && p.BirthTime > new DateTime(1990, 08, 29, 18, 08, 06));//删选
            var studengSelect =
                studentWhere.Select(p => p.Name + " " + p.Heigh.ToString() + " " + p.BirthTime.ToString());//选择



            //  Method2 Query
            var studenglinq =
                from student in students
                where
                    student.Heigh > 20 && student.Heigh < 80 &&
                    student.BirthTime > new DateTime(1990, 08, 29, 18, 08, 06)
                select student.Name + " " + student.Heigh.ToString() + " " + student.BirthTime.ToString();
        }
    }

    class Student
    {
        public string Name { get; set; }
        public int Heigh { get; set; }
        public DateTime BirthTime { get; set; }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值