jueves, 7 de julio de 2016

LINQ Recipe No. 2-16: How to Pick Every nth Element from a Collection

Contents

1. Introduction
2. Keywords
3. Problem
4. Solution
5. Discussion
5.1 Range(int, int) method
5.2 Skip() method
6. Practice: Picking Every nth Element from a Collection
7. Conclusions
8. Literature & Links

1. Introduction

A task like element selection, or more particularly pick every nth element, from a collection is a common problem that frequently appears in other problems such as randomizing, listing, or load distribution. In this LINQ recipe will demonstrate to the programmer how to write an idiomatic LINQ program to query a collection for finding every nth element.

2. Keywords

  • Collection
  • LINQ
  • List
  • Load distribution
  • Query

3. Problem

Pick every nth element from a given sequence (without dividing the index to determine whether to include an element from the collection).

4. Solution

First, it's possible to divide the sequence's count property by the nth element, and then use Skip() method to bypass a specified number of elements in the sequence.

5. Discussion

5.1 Range(int, int) method

Range() method generates a sequence of number for a range. ("Enumerable.Range Method", 2016). For example: 

IEnumerable<int> cubes = Enumerable.Range(1, 10).Select( x => x * x * x);

Here, Range generates the range of numbers from 1 to 10 as counting, i.e., 10 numbers. Then Select() produces the 3rd power for each number from 1 to 10.

5.2 Skip() method

This method is used to bypass a given number of elements, and it returns the remaining elements of the sequence ("Enumerable.Skip(TSource)", 2016).

int[] grades = {53, 61, 97, 91, 89, 71};

IEnumerable lowerGrades = grades.OrderByDescending(g => g)
.Skip(3);

What we get with this expression is lower grades: in the first place, the sequence is sorted in descending order, then with Skip(3) the higher grades -97, 91, and 89- are skipped.

6. Practice: Picking Every nth Element from a Collection

Now it's time to write an idiomatic LINQ query in LINQPad to pick every nth element in a sequence.

With int n = 10; (line 2) we specify the nth element: in this case we will pick every 10th element. In line 5 we request a list of 100 numbers -range 1 to 100.


It's required a data structure, in this case a list, to store the nth elements; that is define in line 8List<int> nthElements = new List<int>();.


With this in mind, we got lines 11-13: here the expression Enumerable.Range(0, numbers.Count()/n) produces a range from 0 to 9; its purpose is allow the iteration of each nth element in the numbers list.


Now the code numbers.Skip(k*n).First() skips the k*n elements in numbers, and chooses the first element of the remaining elements generated by Skip(k*n).


Let's play an execution for this amazing LINQ code: 

7. Conclusions

An operation like pick an element from a collection is a common task; this recipe has showed us how to pick every nth element from a sequence by using the Skip() and First() methods.

The next recipe, the LINQ programmer will learn how to find the larger or smaller of several sequences at each index.

8. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
Enumerable.Range Method (Int32, Int32) (System.Linq) (2016, July 7). Retrieved from: https://msdn.microsoft.com/en-us/library/system.linq.enumerable.range(v=vs.110).aspx
Enumerable.Skip(TSource) Method (IEnumerable(TSource), Int32) (System.Linq) (2016, July 7). Retrieved from: https://msdn.microsoft.com/en-us/library/bb358985%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


V

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.