Filters a sequence based on a predicate.
An IEnumerable<TSource> that contains elements from the input sequence that satisfy the condition.
The Where extension method enables you to query an IEnumerable<TSource> collection and return only the elements that match a specified condition. This method is implemented using deferred execution.
The predicate function takes an element of the source sequence as input and returns a bool value indicating whether the element should be included in the result.
If the source sequence is empty, Where returns an empty sequence.
If the predicate is null, a ArgumentNullException is thrown.
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// Use Where to select numbers greater than 5
IEnumerable<int> numbersGreaterThan5 = numbers.Where(n => n > 5);
Console.WriteLine("Numbers greater than 5:");
foreach (int number in numbersGreaterThan5)
{
Console.Write(number + " ");
}
// Output: Numbers greater than 5: 9 8 6 7
}
}