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; }
}
}
Demo(List 排序,Linq查询)
最新推荐文章于 2022-08-11 09:22:59 发布