2007-12-14 14:13 [C# 3.0] LINQ - Take, Skip, TakeWhile, SkipWhile
Take 用起來類似 SQL Server 的 TOP,傳入參數是 count ,表示要取出前幾筆資料;Skip 語法和 Take 完全相同,但功能完全相反,它會略過前幾筆的資料。
TakeWhile, SkipWhile 可用傳入一個條件來取出前 n 筆的資料。
Take
這範例用 Take 取出前 5 筆資料
執行結果
先選出長度在 4 以內的字串,再取出前 3 筆
執行結果
Skip
這範例會略過前面 3 筆資料
執行結果
取出第 3~4 筆資料(假設從 1 開始算起)
執行結果
這個範例會從前面找出小於 60 的數值,若遇到 60 或 60 以上則停止尋找。
執行結果,因為 90 已經大於 60 了,45 與 33 不會被取出,這和 Where 不相同
TakeWhile
這範例的資料是王建民於 2007/6 到 2006/7 的每場比賽戰績,要得知這兩個月開始是幾連勝,先用 TakeWhile 從頭取出連續不敗的場次(n連不敗),再從不敗場次取出勝場。
PS: "與勝負無關" 不會中斷連勝紀錄,也不會中斷連敗紀錄。
執行結果
TakeWhile, SkipWhile 可用傳入一個條件來取出前 n 筆的資料。
Take
這範例用 Take 取出前 5 筆資料
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
List<string> playerList = new List<string>() { "Kuo", "Wang", "Matsuzaka", "Darvish", "Tsao" , "Park" };
var players = playerList.Take(5);
foreach (var player in players)
Console.WriteLine(player);
}
}
|
Kuo Wang Matsuzaka Darvish Tsao |
先選出長度在 4 以內的字串,再取出前 3 筆
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
List<string> playerList = new List<string>() { "Kuo", "Wang", "Matsuzaka", "Darvish", "Tsao" , "Park" };
var players = (from p in playerList
where p.Length <= 4
select p)
.Take(3);
foreach (var player in players)
Console.WriteLine(player);
}
}
|
Kuo Wang Tsao |
Skip
這範例會略過前面 3 筆資料
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
List<string> playerList = new List<string>() { "Kuo", "Wang", "Matsuzaka", "Darvish", "Tsao" , "Park" };
var players = playerList.Skip(3);
foreach (var player in players)
Console.WriteLine(player);
}
}
|
Darvish Tsao Park |
取出第 3~4 筆資料(假設從 1 開始算起)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
List<string> playerList = new List<string>() { "Kuo", "Wang", "Matsuzaka", "Darvish", "Tsao" , "Park" };
var players = playerList.Skip(2).Take(2);
foreach (var player in players)
Console.WriteLine(player);
}
}
|
Matsuzaka Darvish |
這個範例會從前面找出小於 60 的數值,若遇到 60 或 60 以上則停止尋找。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
int[] scores = { 30, 50, 25, 90, 60, 45, 33, 80 };
var badScores = scores.TakeWhile(s => s < 60);
foreach (var score in badScores)
Console.WriteLine(score);
}
}
|
30 50 25 |
TakeWhile
這範例的資料是王建民於 2007/6 到 2006/7 的每場比賽戰績,要得知這兩個月開始是幾連勝,先用 TakeWhile 從頭取出連續不敗的場次(n連不敗),再從不敗場次取出勝場。
PS: "與勝負無關" 不會中斷連勝紀錄,也不會中斷連敗紀錄。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public enum WinOrLose { Win, Lose, None };
public class GameLog // 戰績類別
{
public string Date; // 比賽日期
public WinOrLose WL; // 勝 or 敗 or 無關勝負
}
class Program
{
static void Main(string[] args)
{
List<GameLog> gameLogList = new List<GameLog>();
gameLogList.Add(new GameLog { Date = "6/01", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "6/06", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "6/12", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "6/17", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "6/23", WL = WinOrLose.None }); // 無關勝負
gameLogList.Add(new GameLog { Date = "6/28", WL = WinOrLose.None }); // 無關勝負
gameLogList.Add(new GameLog { Date = "7/03", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "7/08", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "7/14", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "7/19", WL = WinOrLose.Lose }); // 敗場
gameLogList.Add(new GameLog { Date = "7/24", WL = WinOrLose.Win });
gameLogList.Add(new GameLog { Date = "7/29", WL = WinOrLose.Win });
var continueWin = gameLogList.TakeWhile(g => g.WL != WinOrLose.Lose) // 取出 "n連不敗"
.Where(g => g.WL == WinOrLose.Win); // 從 "n連不敗" 中取出所有的勝場
foreach (var log in continueWin)
Console.WriteLine(log.Date);
Console.WriteLine(continueWin.Count().ToString() + "連勝");
}
}
|
6/01 6/06 6/12 6/17 7/03 7/08 7/14 7連勝 |
by cppbuilder ( cppbuilder@gmail.com )


