若是兩個序列的對應元素相等且這兩個序列具備相同數量的元素,則視這兩個序列相等。sql
SequenceEqual方法經過並行地枚舉兩個數據源並比較相應元素來判斷兩個序列是否相等。若是兩個序列徹底相等,返回true,不然返回false。如下代碼是SequenceEqual方法的實現過程:this
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer) { if (comparer == null) { comparer = EqualityComparer<TSource>.Default; } if (first == null) { throw Error.ArgumentNull("first"); } if (second == null) { throw Error.ArgumentNull("second"); } using (IEnumerator<TSource> enumerator = first.GetEnumerator()) { using (IEnumerator<TSource> enumerator2 = second.GetEnumerator()) { while (enumerator.MoveNext()) { if (!enumerator2.MoveNext() || !comparer.Equals(enumerator.Current, enumerator2.Current)) { return false; } } if (enumerator2.MoveNext()) { return false; } } } return true; }
以上代碼的執行過程爲:spa
1. 若是比較器爲null,賦值爲默認值EqualityComparer<TSource>.Default。code
2. 若是序列1爲null,拋出異常。blog
3. 若是序列2爲null,拋出異常。接口
4. 遍歷序列1。在此過程當中,若是序列2到達底端則返回false;若是序列1的當前值與序列2的當前值不一樣,則返回false。ip
5. 序列1遍歷完成後,若是序列2未到達底端,則返回false。字符串
6. 若是第2-5步都沒有執行,則返回true。get
限定符運算返回一個 Boolean 值,該值指示序列中是否有一些元素知足條件或是否全部元素都知足條件。string
下圖描述了兩個不一樣源序列上的兩個不一樣限定符運算。第一個運算詢問是否有一個或多個元素爲字符「A」,結果爲 true。第二個運算詢問是否全部元素都爲字符「A」,結果爲 true。
All方法用來肯定是否序列中的全部元素都知足條件。如下代碼演示了All的用法:
string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" }; string[] source2 = new string[] { "A", "A", "A", "A", "A", "A" }; Console.WriteLine(source1.All(w => w == "A")); //console will print "False" Console.WriteLine(source2.All(w => w == "A")); //console will print "True"
2. Any
Any方法的無參方式用來肯定序列是否包含任何元素。若是源序列包含元素,則爲 true;不然爲 false。
Any方法的有參方式用來肯定序列中是否有元素知足條件。只要有一個元素符合指定條件即返回true,若是一個符合指定條件的元素都沒有則返回false。如下代碼演示了Any方法有參方式的用法:
string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" };
string[] source2 = new string[] { "A", "A", "A", "A", "A", "A" };
Console.WriteLine(source1.Any(w => w == "A")); //console will print "True"
Console.WriteLine(source2.Any(w => w == "A")); //console will print "True"
Contains方法用來肯定序列是否包含知足指定條件的元素。若是有返回true,不然返回false。如下代碼使用默認的String比較器來判斷序列中是否含有指定的字符串:
string[] source1 = new string[] { "A", "B", "C", "D", "E", "F" }; Console.WriteLine(source1.Contains("A")); //console will print "True" Console.WriteLine(source1.Contains("G")); //console will print "False"
若是要對序列中的元素進行自定義比較,須要一個IEqualityComparer<T>接口的實現類做爲比較器,用於比較序列中的元素。
LINQ 中的分區指的是在不從新排列元素的狀況下,將輸入序列劃分爲兩部分,而後返回其中一個部分的操做。
下圖顯示對一個字符序列執行三個不一樣的分區操做的結果。第一個操做返回序列中的前三個元素。第二個操做跳過前三個元素,返回剩餘的元素。第三個操做跳過序列中的前兩個元素,返回接下來的三個元素。
Take(int n)方法將從序列的開頭返回數量爲n的連續元素。如下代碼演示了從一個序列中返回其前五個元素:
int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; var q = source.Take(5); foreach (var item in q) { Console.WriteLine(item); }
上述代碼的運行結果爲下圖所示:
var query = db.Employees .Take(10) .ToList(); 查詢sql: SELECT TOP (10) [c].[EmployeeID] AS [EmployeeID], [c].[LastName] AS [LastName], [c].[FirstName] AS [FirstName], [c].[Title] AS [Title], [c].[TitleOfCourtesy] AS [TitleOfCourtesy], [c].[BirthDate] AS [BirthDate], [c].[HireDate] AS [HireDate], [c].[Address] AS [Address], [c].[City] AS [City], [c].[Region] AS [Region], [c].[PostalCode] AS [PostalCode], [c].[Country] AS [Country], [c].[HomePhone] AS [HomePhone], [c].[Extension] AS [Extension], [c].[Photo] AS [Photo], [c].[Notes] AS [Notes], [c].[ReportsTo] AS [ReportsTo], [c].[PhotoPath] AS [PhotoPath] FROM [dbo].[Employees] AS [c]
TakeWhile方法執行時將逐個比較序列中的每一個元素是否知足指定條件,直到碰到不符合指定的條件的元素時,返回前面全部的元素組成的序列。如下代碼演示了這一過程:
int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; var q = source.TakeWhile(i => i < 100); foreach (var item in q) { Console.WriteLine(item); }
上述代碼的運行結果爲下圖所示:
Skip(int n)方法將跳過序列開頭的n個元素,而後返回其他的連續元素。如下代碼演示了從一個序列中跳過前五個元素,而後返回其他的元素組成的序列:
int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; var q = source.Skip(5); foreach (var item in q) { Console.WriteLine(item); }
上述代碼的運行結果爲下圖所示:
var query = db.Employees .OrderBy(e => e.EmployeeID) .Skip(5) .ToList(); 查詢sql: SELECT [Extent1].[EmployeeID] AS [EmployeeID], [Extent1].[LastName] AS [LastName], [Extent1].[FirstName] AS [FirstName], [Extent1].[Title] AS [Title], [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy], [Extent1].[BirthDate] AS [BirthDate], [Extent1].[HireDate] AS [HireDate], [Extent1].[Address] AS [Address], [Extent1].[City] AS [City], [Extent1].[Region] AS [Region], [Extent1].[PostalCode] AS [PostalCode], [Extent1].[Country] AS [Country], [Extent1].[HomePhone] AS [HomePhone], [Extent1].[Extension] AS [Extension], [Extent1].[Photo] AS [Photo], [Extent1].[Notes] AS [Notes], [Extent1].[ReportsTo] AS [ReportsTo], [Extent1].[PhotoPath] AS [PhotoPath] FROM ( SELECT [Extent1].[EmployeeID] AS [EmployeeID], [Extent1].[LastName] AS [LastName], [Extent1].[FirstName] AS [FirstName], [Extent1].[Title] AS [Title], [Extent1].[TitleOfCourtesy] AS [TitleOfCourtesy], [Extent1].[BirthDate] AS [BirthDate], [Extent1].[HireDate] AS [HireDate], [Extent1].[Address] AS [Address], [Extent1].[City] AS [City], [Extent1].[Region] AS [Region], [Extent1].[PostalCode] AS [PostalCode], [Extent1].[Country] AS [Country], [Extent1].[HomePhone] AS [HomePhone], [Extent1].[Extension] AS [Extension], [Extent1].[Photo] AS [Photo], [Extent1].[Notes] AS [Notes], [Extent1].[ReportsTo] AS [ReportsTo], [Extent1].[PhotoPath] AS [PhotoPath], row_number() OVER (ORDER BY [Extent1].[EmployeeID] ASC) AS [row_number] FROM [dbo].[Employees] AS [Extent1] ) AS [Extent1] WHERE [Extent1].[row_number] > 5 ORDER BY [Extent1].[EmployeeID] ASC
SkipWhile方法執行時將逐個比較序列中的每一個元素是否知足指定條件,直到碰到不符合指定的條件的元素時,返回其他全部的元素組成的序列。如下代碼演示了這一過程:
int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 }; var q = source.SkipWhile(i => i < 100); foreach (var item in q) { Console.WriteLine(item); }
上述代碼的運行結果爲下圖所示: