179-linq-restriction-operator-where-clause-indexed-in-c

LINQ restriction operator where is very useful when we want to filter or restrict some data. For example, if we want to filter all numbers higher or equal 3 by using LINQ restriction operator where here is an example:

int[] numbers = { 5, 4, 1, 3, 6, 2, 0 };
int[] filtered = numbers.Where((i, ind) => i >= 3).ToArray();

If we want to use indexed data from an array, LINQ where indexed in C# is used as follows:

int[] numbers = { 5, 4, 1, 3, 6, 2, 0 };
int[] everySecond = numbers.Where((i, ind) => ind % 2 == 0).ToArray();

If we want to restrict the data with several conditions, such as “find elements higher or equal 3 among every second element” where clause would be as follows:

int[] filtered = numbers.Where((i, ind) => (i >= 3) && (ind % 2 == 0)).ToArray();

So data extraction with given criterion is easy with LINQ restriction operator where.

External links:

LINQ Restriction Operator Where on Microsoft
LINQ Restriction Operator Where on C-sharpcorner
LINQ Restriction Operator Where on Batches.wikidot.com

Posted in c-sharp-code-examples

Leave a Reply

Your email address will not be published. Required fields are marked *

*