码迷,mamicode.com
首页 > 其他好文 > 详细

Lamabda Where Select Find First等区别

时间:2015-01-18 13:00:45      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 

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; }
    }
}

 

Lamabda Where Select Find First等区别

标签:

原文地址:http://www.cnblogs.com/mtsl/p/4231626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!