Quantcast Partitioning operators trong Linq

Partitioning operators trong Linq

by Neon Quach 21. February 2011 22:20

Trong bài blog này mình sẽ giới thiệu các toán tử phân chia (Partitioning operators) trong Linq bao gồm: Take, Skip, TakeWhile và SkipWhile.

Take
Toán tử Take trả về số elements từ 1 sequence bởi 1 số cho trước, và đây là signature của toán tử Take:

        //
        // Summary:
        //     Returns a specified number of contiguous elements from the start of a sequence.
        //
        // Parameters:
        //   source:
        //     The sequence to return elements from.
        //
        //   count:
        //     The number of elements to return.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of source.
        //
        // Returns:
        //     An System.Collections.Generic.IEnumerable<T> that contains the specified
        //     number of elements from the start of the input sequence.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     source is null.
        public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count);

Code example:

using System;
using System.Linq;

namespace BasicLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            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);
            }
            Console.Read();
        }
    }
}


Kết quả:
86
27794
100

Skip

Toán tử Skip loại bỏ các elements từ 1 số cho trước, sau đó lấy các elements còn lại.

Singature của toán tử Skip

        //
        // Summary:
        //     Bypasses a specified number of elements in a sequence and then returns the
        //     remaining elements.
        //
        // Parameters:
        //   source:
        //     An System.Collections.Generic.IEnumerable<T> to return elements from.
        //
        //   count:
        //     The number of elements to skip before returning the remaining elements.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of source.
        //
        // Returns:
        //     An System.Collections.Generic.IEnumerable<T> that contains the elements that
        //     occur after the specified index in the input sequence.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     source is null.
        public static IEnumerable<TSource> Skip<TSource>(this IEnumerable<TSource> source, int count);

Code example:

using System;
using System.Linq;

namespace BasicLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            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);
            }
            Console.Read();
        }
    }
}


TakeWhile


Toán tử TakeWhile sẽ lấy các elements nếu điều kiện truyền vào phương thức là True (loại bỏ các elements còn lại trong 1 sequense)

Signature của toán tử TakeWhile

        //
        // Summary:
        //     Returns elements from a sequence as long as a specified condition is true.
        //
        // Parameters:
        //   source:
        //     A sequence to return elements from.
        //
        //   predicate:
        //     A function to test each element for a condition.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of source.
        //
        // Returns:
        //     An System.Collections.Generic.IEnumerable<T> that contains the elements from
        //     the input sequence that occur before the element at which the test no longer
        //     passes.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     source or predicate is null.
        public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Code example

using System;
using System.Linq;

namespace BasicLinq
{
    class Program
    {
        static void Main()
        {
            int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 };
            var q = source.TakeWhile(n => n < 100);
            foreach (var item in q)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Out Put:
86
2
77
94

Phương thức mở rộng TakeWhile (toán tử) sẽ capture tham số truyền vào và trả về 1 đối tượng enumerable, sẽ quăng biệt lệ ArgumentNullException nếu tham số truyền vào là null. Tham số truyền vào là 1 hàm được xác nhận định nghĩa trước (predicate function), các elements sẽ được giữ lại nếu kết quả kiểm tra là True và sẽ dừng lại nếu kết quả là false.

SkipWhile

Toán tử SkipWhile: khác với TakeWhile, SkipWhile sẽ bỏ qua những elements nào match với điều kiện truyền vào hàm, quá trình kiểm tra sẽ dừng lại nếu điều kiện truyền vào hàm là True.

Signature method:

        //
        // Summary:
        //     Bypasses elements in a sequence as long as a specified condition is true
        //     and then returns the remaining elements.
        //
        // Parameters:
        //   source:
        //     An System.Collections.Generic.IEnumerable<T> to return elements from.
        //
        //   predicate:
        //     A function to test each element for a condition.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of source.
        //
        // Returns:
        //     An System.Collections.Generic.IEnumerable<T> that contains the elements from
        //     the input sequence starting at the first element in the linear series that
        //     does not pass the test specified by predicate.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     source or predicate is null.
        public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
        //
        // Summary:
        //     Bypasses elements in a sequence as long as a specified condition is true
        //     and then returns the remaining elements. The element's index is used in the
        //     logic of the predicate function.
        //
        // Parameters:
        //   source:
        //     An System.Collections.Generic.IEnumerable<T> to return elements from.
        //
        //   predicate:
        //     A function to test each source element for a condition; the second parameter
        //     of the function represents the index of the source element.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of source.
        //
        // Returns:
        //     An System.Collections.Generic.IEnumerable<T> that contains the elements from
        //     the input sequence starting at the first element in the linear series that
        //     does not pass the test specified by predicate.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     source or predicate is null.
        public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, intbool> predicate);


Code Example:

using System;
using System.Linq;

namespace BasicLinq
{
    class Program
    {
        static void Main()
        {
            int[] source = new int[] { 86, 2, 77, 94, 100, 65, 5, 22, 70, 55, 81, 66, 45 };
            var q = source.SkipWhile(n => n < 100);
            foreach (var item in q)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Out Put:
100
65
5
22
70
55
81
66
45

Tags: ,


Categories: linq | c#

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers