【转】LINQ标准查询操作符(五)

Source

转自:
http://www.cnblogs.com/dotnetmvc/p/3680650.html

十二、相等操作符

如果两个序列的对应元素相等且这两个序列具有相同数量的元素,则视这两个序列相等。
SequenceEqual方法通过并行地枚举两个数据源并比较相应元素来判断两个序列是否相等。如果两个序列完全相等,返回true,否则返回false。以下代码是SequenceEqual方法的实现过程:

public static bool SequenceEqual(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
{
if (comparer == null)
{
comparer = EqualityComparer.Default;
}
if (first == null)
{
throw Error.ArgumentNull(“first”);
}
if (second == null)
{
throw Error.ArgumentNull(“second”);
}
using (IEnumerator enumerator = first.GetEnumerator())
{
using (IEnumerator enumerator2 = second.GetEnumerator())
{
while (enumerator.MoveNext())
{
if (!enumerator2.MoveNext() || !comparer.Equals(enumerator.Current, enumerator2.Current))
{
return false;
}
}
if (enumerator2.MoveNext())
{
return false;
}
}
}
return true;
}

以上代码的执行过程为:

  1. 如果比较器为null,赋值为默认值EqualityComparer.Default。
  2. 如果序列1为null,抛出异常。
  3. 如果序列2为null,抛出异常。
  4. 遍历序列1。在此过程中,如果序列2到达底端则返回false;如果序列1的当前值与序列2的当前值不同,则返回false。
  5. 序列1遍历完成后,如果序列2未到达底端,则返回false。
  6. 如果第2-5步都没有执行,则返回true。

十三、限定操作符

限定符运算返回一个 Boolean 值,该值指示序列中是否有一些元素满足条件或是否所有元素都满足条件。
下图描述了两个不同源序列上的两个不同限定符运算。第一个运算询问是否有一个或多个元素为字符“A”,结果为 true。第二个运算询问是否所有元素都为字符“A”,结果为 true。

1. All

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”

3. Contains

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接口的实现类作为比较器,用于比较序列中的元素。

十四、分区操作符

LINQ 中的分区指的是在不重新排列元素的情况下,将输入序列划分为两部分,然后返回其中一个部分的操作。
下图显示对一个字符序列执行三个不同的分区操作的结果。第一个操作返回序列中的前三个元素。第二个操作跳过前三个元素,返回剩余的元素。第三个操作跳过序列中的前两个元素,返回接下来的三个元素。

1. Take

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]

2. TakeWhile

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

上述代码的运行结果为下图所示:

3. Skip

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

  1. SkipWhile
    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);
}

上述代码的运行结果为下图所示: