码迷,mamicode.com
首页 > Windows程序 > 详细

C# Lambda 实例

时间:2016-01-18 12:13:09      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace lambda
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        public static List<Person> PersonsList()
        {
            List<Person> persons = new List<Person>();
            for (int i = 0; i < 7; i++)
            {
                Person p = new Person() { Name = i + "-Son", Age = 8 - i, };
                persons.Add(p);
            }
            return persons;
        }

        static void Main(string[] args)
        {
            List<Person> persons = PersonsList();
            persons = persons.Where(p => p.Age > 6).ToList();       //所有Age>6的Person的集合
            Person per = persons.SingleOrDefault(p => p.Age == 1);  //Age=1的单个people类
            persons = persons.Where(p => p.Name.Contains("Son")).ToList();   //所有Name包含儿子的Person的集合
            Console.ReadLine();
        }
    }
}

C# Lambda 实例

标签:

原文地址:http://www.cnblogs.com/xiangkezhi-maiku/p/5138619.html

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