Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to filter array elements based on a predicate
In C#, filtering array elements based on a predicate allows you to select elements that meet specific conditions. A predicate is a function that returns true or false for each element, determining whether it should be included in the result.
The most common approach is using LINQ's Where method, which applies a predicate function to filter elements. You can also use traditional loops or the Array.FindAll method for filtering.
Syntax
Following is the syntax for filtering with LINQ's Where method −
IEnumerable<T> result = array.Where(element => condition);
Following is the syntax for filtering with Array.FindAll −
T[] result = Array.FindAll(array, element => condition);
Using LINQ Where Method
The Where method is the most flexible approach for filtering arrays. It accepts a lambda expression as a predicate −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] arr = { 40, 42, 12, 83, 75, 40, 95 };
Console.WriteLine("Original Array:");
foreach (int a in arr) {
Console.WriteLine(a);
}
// Filter elements above 50
IEnumerable<int> filtered = arr.Where(a => a >= 50);
Console.WriteLine("\nElements above or equal to 50:");
foreach (int res in filtered) {
Console.WriteLine(res);
}
}
}
The output of the above code is −
Original Array: 40 42 12 83 75 40 95 Elements above or equal to 50: 83 75 95
Using Array.FindAll Method
The Array.FindAll method returns a new array containing elements that match the predicate −
using System;
public class Demo {
public static void Main() {
string[] names = { "Alice", "Bob", "Charlie", "David", "Eve" };
Console.WriteLine("Original Names:");
foreach (string name in names) {
Console.WriteLine(name);
}
// Filter names with length greater than 4
string[] longNames = Array.FindAll(names, name => name.Length > 4);
Console.WriteLine("\nNames with more than 4 characters:");
foreach (string name in longNames) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Original Names: Alice Bob Charlie David Eve Names with more than 4 characters: Alice Charlie David
Multiple Conditions and Complex Predicates
You can combine multiple conditions using logical operators in the predicate −
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] numbers = { 15, 23, 8, 42, 67, 19, 88, 5, 34 };
Console.WriteLine("Original Numbers:");
Console.WriteLine(string.Join(", ", numbers));
// Filter even numbers greater than 20
var filtered = numbers.Where(n => n % 2 == 0 && n > 20);
Console.WriteLine("\nEven numbers greater than 20:");
Console.WriteLine(string.Join(", ", filtered));
// Filter numbers between 10 and 50
var rangeFiltered = numbers.Where(n => n >= 10 && n <= 50);
Console.WriteLine("\nNumbers between 10 and 50:");
Console.WriteLine(string.Join(", ", rangeFiltered));
}
}
The output of the above code is −
Original Numbers: 15, 23, 8, 42, 67, 19, 88, 5, 34 Even numbers greater than 20: 42, 88, 34 Numbers between 10 and 50: 15, 23, 42, 19, 34
Comparison of Filtering Methods
| Method | Return Type | Performance | Use Case |
|---|---|---|---|
| LINQ Where | IEnumerable<T> | Lazy evaluation | Complex queries, chaining operations |
| Array.FindAll | T[] | Immediate execution | Simple filtering, need array result |
| For loop | Custom collection | Best for large datasets | Custom filtering logic, performance critical |
Conclusion
Filtering arrays in C# using predicates provides powerful ways to select elements based on conditions. LINQ's Where method offers flexibility and lazy evaluation, while Array.FindAll provides immediate results as arrays. Choose the method that best fits your performance needs and result type requirements.
