Enumerable 的擴充方法 - Take
用來帶出前N筆資料,並回傳該集合。
如下範例,帶出前2筆資料
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Person> personCollection = new List<Person>();
personCollection.Add(new Person(1, "Tim"));
personCollection.Add(new Person(2, "Brooke"));
personCollection.Add(new Person(3, "John"));
IEnumerable<Person> personIEnumerable = new List<Person>();
personIEnumerable = personCollection.Take(2);
foreach (var item in personIEnumerable)
{
Console.WriteLine("id=" + item.id + " , name=" + item.name);
}
Console.ReadKey();
}
}
class Person
{
public int id { get; set; }
public string name { get; set; }
public Person(int id, string name)
{
this.id = id;
this.name = name;
}
}
}