using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
using System.IO;
public class Program
{
public static void Main(string[] args)
{
List<Student> list = new List<Student>() { new Student() { Name = "张三", Age = 8 }, new Student() { Name = "李四", Age = 9 } };
// 满足条件的IEnumerable<T>
IEnumerable<Student> x1 = list.Where(p => p.Age > 0);
// 满足条件的IEnumerable<string>
IEnumerable<string> x2 = list.Select(p => p.Name);
// 序列中的第一个元素,如果没有满足,则返回default(Student)
Student x3 = list.Find(p => p.Age < 0);
// 序列中的所有元素List<T>,如果没有满足,则返回new List<T>
List<Student> x4 = list.FindAll(p => p.Age < 0);
// 序列中的第一个元素,如果list==new List<T>(),则抛异常
Student x5 = list.First();
// 序列中第一个元素,或是default(T)
Student x6 = list.FirstOrDefault();
list = new List<Student>();
x1 = list.Where(p => p.Age > 0);
x2 = list.Select(p => p.Name);
x3 = list.Find(p => p.Age < 0);
x4 = list.FindAll(p => p.Age < 0);
x5 = list.First();
x6 = list.FirstOrDefault();
// list==null,全部抛异常
list = null;
x1 = list.Where(p => p.Age > 0);
x2 = list.Select(p => p.Name);
x3 = list.Find(p => p.Age < 0);
x4 = list.FindAll(p => p.Age < 0);
x5 = list.First();
x6 = list.FirstOrDefault();
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
}