System.Linq.Enumerable Class

Namespace: System.Linq

Represents a set of static extension methods for the IEnumerable<T> interface. These methods implement the query operators for LINQ to Objects.

Summary of Members

Methods

Aggregate

Applies an accumulator function over a sequence.

public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);

Example

This example uses the Aggregate method to sum the elements of an array.

int[] numbers = { 1, 2, 3, 4, 5 };
int sum = numbers.Aggregate(0, (current, next) => current + next);
// sum will be 15

All

Determines whether all elements of a sequence satisfy a condition.

public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example uses the All method to check if all numbers in an array are positive.

int[] numbers = { 1, 2, 3, 4, 5 };
bool allPositive = numbers.All(n => n > 0);
// allPositive will be true

Any

Determines whether any element of a sequence satisfies a condition.

public static bool Any<TSource>(this IEnumerable<TSource> source);
public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example uses the Any method to check if any number in an array is even.

int[] numbers = { 1, 3, 5, 7, 9 };
bool hasEven = numbers.Any(n => n % 2 == 0);
// hasEven will be false

Append

Appends a value to the end of the sequence.

public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element);

Example

This example uses the Append method to add an element to the end of a list.

List<int> numbers = new List<int> { 1, 2, 3 };
IEnumerable<int> newNumbers = numbers.Append(4);
// newNumbers will contain { 1, 2, 3, 4 }

Average

Computes the average of a sequence of numeric values.

public static double Average(this IEnumerable<int> source);
public static double Average(this IEnumerable<int?> source);
public static decimal Average(this IEnumerable<decimal> source);

Example

This example uses the Average method to calculate the average of an array of integers.

int[] numbers = { 10, 20, 30, 40, 50 };
double average = numbers.Average();
// average will be 30.0

Cast

Casts the elements of the IEnumerable to the specified type.

public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source);

Example

This example uses Cast to convert an array of objects to an array of strings.

object[] objects = { "apple", "banana", "cherry" };
IEnumerable<string> strings = objects.Cast<string>();
// strings will contain { "apple", "banana", "cherry" }

Chunk

Splits the sequence into chunks of a specified size.

public static IEnumerable<TSource[]> Chunk<TSource>(this IEnumerable<TSource> source, int size);

Example

This example uses Chunk to split a list into chunks of 3.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
IEnumerable<int[]> chunks = numbers.Chunk(3);
// chunks will contain { {1,2,3}, {4,5,6}, {7,8,9}, {10} }

Concat

Concatenates two sequences.

public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);

Example

This example concatenates two lists of integers.

List<int> list1 = new List<int> { 1, 2 };
List<int> list2 = new List<int> { 3, 4 };
IEnumerable<int> combined = list1.Concat(list2);
// combined will contain { 1, 2, 3, 4 }

Contains

Determines whether a sequence contains a specified element.

public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value);
public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer);

Example

This example checks if a list contains the number 5.

List<int> numbers = new List<int> { 1, 2, 3, 4 };
bool hasFive = numbers.Contains(5);
// hasFive will be false

Count

Returns the number of elements in a sequence.

public static int Count<TSource>(this IEnumerable<TSource> source);
public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example counts the total number of elements and the number of even elements in an array.

int[] numbers = { 1, 2, 3, 4, 5, 6 };
int totalCount = numbers.Count(); // 6
int evenCount = numbers.Count(n => n % 2 == 0); // 3

DefaultIfEmpty

Returns the elements of the specified sequence or a single value if the sequence is empty.

public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable<TSource> source);
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable<TSource> source, TSource defaultValue);

Example

This example returns a default value if the sequence is empty.

List<int> emptyList = new List<int>();
IEnumerable<int> result = emptyList.DefaultIfEmpty(-1);
// result will contain { -1 }

List<int> numbers = new List<int> { 1, 2 };
IEnumerable<int> result2 = numbers.DefaultIfEmpty(-1);
// result2 will contain { 1, 2 }

Distinct

Returns distinct elements from a sequence.

public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source);
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);

Example

This example returns only the unique elements from a list.

List<int> numbers = new List<int> { 1, 2, 2, 3, 1, 4 };
IEnumerable<int> distinctNumbers = numbers.Distinct();
// distinctNumbers will contain { 1, 2, 3, 4 }

Except

Produces the set difference of two sequences.

public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

Example

This example finds the elements in the first list that are not in the second.

List<int> list1 = new List<int> { 1, 2, 3, 4 };
List<int> list2 = new List<int> { 3, 4, 5, 6 };
IEnumerable<int> difference = list1.Except(list2);
// difference will contain { 1, 2 }

First

Returns the first element of a sequence.

public static TSource First<TSource>(this IEnumerable<TSource> source);
public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example gets the first element and the first even element from an array.

int[] numbers = { 1, 2, 3, 4, 5 };
int firstElement = numbers.First(); // 1
int firstEven = numbers.First(n => n % 2 == 0); // 2

FirstOrDefault

Returns the first element of a sequence, or a default value if the sequence is empty.

public static TSource? FirstOrDefault<TSource>(this IEnumerable<TSource> source);
public static TSource? FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example gets the first element, returning null if the sequence is empty.

List<int> emptyList = new List<int>();
int? first = emptyList.FirstOrDefault(); // null

List<int> numbers = new List<int> { 1, 2, 3 };
int? firstNonNull = numbers.FirstOrDefault(); // 1

GroupBy

Groups the elements of a sequence according to a specified key selector.

public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector);

Example

This example groups numbers by whether they are even or odd.

int[] numbers = { 1, 2, 3, 4, 5, 6 };
var groups = numbers.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");
// groups will contain two groups: "Even" {2, 4, 6} and "Odd" {1, 3, 5}

GroupJoin

Correlates the elements of two sequences based on key equality and groups the results.

public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector);

Example

This example performs a group join between departments and employees.

var departments = new[] {
    new { Id = 1, Name = "Sales" },
    new { Id = 2, Name = "IT" }
};
var employees = new[] {
    new { Name = "Alice", DeptId = 1 },
    new { Name = "Bob", DeptId = 1 },
    new { Name = "Charlie", DeptId = 2 }
};

var result = departments.GroupJoin(
    employees,
    d => d.Id,
    e => e.DeptId,
    (d, emps) => new { Department = d.Name, Employees = emps.Select(e => e.Name) }
);
// result will contain { { Department = "Sales", Employees = {"Alice", "Bob"} }, { Department = "IT", Employees = {"Charlie"} } }

Intersect

Produces the set intersection of two sequences.

public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

Example

This example finds the common elements between two lists.

List<int> list1 = new List<int> { 1, 2, 3, 4 };
List<int> list2 = new List<int> { 3, 4, 5, 6 };
IEnumerable<int> intersection = list1.Intersect(list2);
// intersection will contain { 3, 4 }

Join

Correlates the elements of two sequences based on matching keys.

public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector);

Example

This example performs a join between departments and employees based on Department ID.

var departments = new[] {
    new { Id = 1, Name = "Sales" },
    new { Id = 2, Name = "IT" }
};
var employees = new[] {
    new { Name = "Alice", DeptId = 1 },
    new { Name = "Bob", DeptId = 1 },
    new { Name = "Charlie", DeptId = 2 }
};

var result = departments.Join(
    employees,
    d => d.Id,
    e => e.DeptId,
    (d, e) => new { DepartmentName = d.Name, EmployeeName = e.Name }
);
// result will contain
// { { DepartmentName = "Sales", EmployeeName = "Alice" },
//   { DepartmentName = "Sales", EmployeeName = "Bob" },
//   { DepartmentName = "IT", EmployeeName = "Charlie" } }

Last

Returns the last element of a sequence.

public static TSource Last<TSource>(this IEnumerable<TSource> source);
public static TSource Last<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example gets the last element and the last odd element from an array.

int[] numbers = { 1, 2, 3, 4, 5 };
int lastElement = numbers.Last(); // 5
int lastOdd = numbers.Last(n => n % 2 != 0); // 5

LastOrDefault

Returns the last element of a sequence, or a default value if the sequence is empty.

public static TSource? LastOrDefault<TSource>(this IEnumerable<TSource> source);
public static TSource? LastOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example gets the last element, returning null if the sequence is empty.

List<int> emptyList = new List<int>();
int? last = emptyList.LastOrDefault(); // null

List<int> numbers = new List<int> { 1, 2, 3 };
int? lastNonNull = numbers.LastOrDefault(); // 3

Max

Returns the maximum value in a sequence of values.

public static TSource Max<TSource>(this IEnumerable<TSource> source);
public static TResult Max<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

Example

This example finds the maximum number in an array.

int[] numbers = { 5, 2, 8, 1, 9 };
int maxNumber = numbers.Max(); // 9

Min

Returns the minimum value in a sequence of values.

public static TSource Min<TSource>(this IEnumerable<TSource> source);
public static TResult Min<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

Example

This example finds the minimum number in an array.

int[] numbers = { 5, 2, 8, 1, 9 };
int minNumber = numbers.Min(); // 1

OfType

Filters a sequence of values based on a specified type.

public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source);

Example

This example filters a list of objects to get only the strings.

object[] mixed = { 1, "hello", 3.14, "world", 42 };
IEnumerable<string> strings = mixed.OfType<string>();
// strings will contain { "hello", "world" }

OrderBy

Sorts the elements of a sequence in ascending order.

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer);

Example

This example sorts a list of strings alphabetically.

List<string> names = new List<string> { "Charlie", "Alice", "Bob" };
IEnumerable<string> sortedNames = names.OrderBy(n => n);
// sortedNames will contain { "Alice", "Bob", "Charlie" }

OrderByDescending

Sorts the elements of a sequence in descending order.

public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer);

Example

This example sorts a list of numbers in descending order.

int[] numbers = { 5, 2, 8, 1, 9 };
IEnumerable<int> sortedNumbers = numbers.OrderByDescending(n => n);
// sortedNumbers will contain { 9, 8, 5, 2, 1 }

Prepend

Adds a value to the beginning of the sequence.

public static IEnumerable<TSource> Prepend<TSource>(this IEnumerable<TSource> source, TSource element);

Example

This example adds an element to the beginning of a list.

List<int> numbers = new List<int> { 2, 3, 4 };
IEnumerable<int> newNumbers = numbers.Prepend(1);
// newNumbers will contain { 1, 2, 3, 4 }

Range

Generates a sequence of integral numbers within a specified range.

public static IEnumerable<int> Range(int start, int count);

Example

This example generates a sequence of 5 integers starting from 1.

IEnumerable<int> numbers = Enumerable.Range(1, 5);
// numbers will contain { 1, 2, 3, 4, 5 }

Repeat

Generates a sequence that contains one repeated value.

public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count);

Example

This example generates a sequence of three "hello" strings.

IEnumerable<string> greetings = Enumerable.Repeat("hello", 3);
// greetings will contain { "hello", "hello", "hello" }

Reverse

Inverts the order of the elements in a sequence.

public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source);

Example

This example reverses the order of elements in an array.

int[] numbers = { 1, 2, 3, 4, 5 };
IEnumerable<int> reversedNumbers = numbers.Reverse();
// reversedNumbers will contain { 5, 4, 3, 2, 1 }

Select

Projects each element of a sequence into a new form.

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

Example

This example squares each number in an array.

int[] numbers = { 1, 2, 3, 4, 5 };
IEnumerable<int> squares = numbers.Select(n => n * n);
// squares will contain { 1, 4, 9, 16, 25 }

SelectMany

Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector);
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector);

Example

This example flattens a list of lists into a single list.

List<List<int>> listOfLists = new List<List<int>>
{
    new List<int> { 1, 2 },
    new List<int> { 3, 4 },
    new List<int> { 5 }
};
IEnumerable<int> flattenedList = listOfLists.SelectMany(list => list);
// flattenedList will contain { 1, 2, 3, 4, 5 }

SequenceEqual

Determines whether two sequences are equal.

public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

Example

This example checks if two lists of integers are equal.

List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 1, 2, 3 };
bool areEqual = list1.SequenceEqual(list2); // true

List<int> list3 = new List<int> { 1, 2, 4 };
bool areEqual2 = list1.SequenceEqual(list3); // false

Single

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

public static TSource Single<TSource>(this IEnumerable<TSource> source);
public static TSource Single<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example gets the single element from a sequence containing only one item.

int[] singleElementArray = { 42 };
int value = singleElementArray.Single(); // 42

int[] multipleElements = { 1, 2 };
// Calling Single() on multipleElements would throw an exception.

SingleOrDefault

Returns the only element of a sequence, or a default value if the sequence is empty. Throws an exception if there is more than one element in the sequence.

public static TSource? SingleOrDefault<TSource>(this IEnumerable<TSource> source);
public static TSource? SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

This example gets the single element, returning null if the sequence is empty.

List<int> emptyList = new List<int>();
int? single = emptyList.SingleOrDefault(); // null

int[] singleElementArray = { 42 };
int? value = singleElementArray.SingleOrDefault(); // 42

int[] multipleElements = { 1, 2 };
// Calling SingleOrDefault() on multipleElements would throw an exception.

Skip

Bypasses a specified number of elements in a sequence and then returns the remaining elements.

public static IEnumerable<TSource> Skip<TSource>(this IEnumerable<TSource> source, int count);

Example

This example skips the first 3 elements of an array.

int[] numbers = { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> skippedNumbers = numbers.Skip(3);
// skippedNumbers will contain { 4, 5, 6 }

SkipWhile

Bypasses elements in a sequence as long as a specified condition is met and then returns the remaining elements.

public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

Example

This example skips numbers while they are less than 5.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
IEnumerable<int> skipped = numbers.SkipWhile(n => n < 5);
// skipped will contain { 5, 6, 7 }

Sum

Computes the sum of a sequence of numeric values.

public static int Sum(this IEnumerable<int> source);
public static decimal Sum(this IEnumerable<decimal> source);
public static TResult Sum<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

Example

This example calculates the sum of an array of integers.

int[] numbers = { 1, 2, 3, 4, 5 };
int sum = numbers.Sum();
// sum will be 15

Take

Returns a specified number of contiguous elements from the start of a sequence.

public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count);

Example

This example takes the first 3 elements of an array.

int[] numbers = { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> takenNumbers = numbers.Take(3);
// takenNumbers will contain { 1, 2, 3 }

TakeWhile

Returns elements of a sequence as long as a specified condition is met.

public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

Example

This example takes numbers while they are less than 5.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
IEnumerable<int> taken = numbers.TakeWhile(n => n < 5);
// taken will contain { 1, 2, 3, 4 }

ThenBy

Performs a subsequent ordering of the elements in a sequence in ascending order.

public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer);

Example

This example sorts people first by age, then by name.

var people = new[] {
    new { Name = "Alice", Age = 30 },
    new { Name = "Bob", Age = 25 },
    new { Name = "Charlie", Age = 30 }
};
var sortedPeople = people.OrderBy(p => p.Age).ThenBy(p => p.Name);
// sortedPeople will be ordered as: Bob (25), Alice (30), Charlie (30)

ThenByDescending

Performs a subsequent ordering of the elements in a sequence in descending order.

public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer);

Example

This example sorts people first by age descending, then by name.

var people = new[] {
    new { Name = "Alice", Age = 30 },
    new { Name = "Bob", Age = 25 },
    new { Name = "Charlie", Age = 30 }
};
var sortedPeople = people.OrderByDescending(p => p.Age).ThenByDescending(p => p.Name);
// sortedPeople will be ordered as: Charlie (30), Alice (30), Bob (25)

ToUnicode

Converts a string to its Unicode representation.

public static string ToUnicode(string s);

Example

This example converts a string to its Unicode escape sequence representation.

string text = "Hello";
string unicodeText = Enumerable.ToUnicode(text);
// unicodeText will be "Hello" (for ASCII, it's the same)

string emoji = "😊";
string unicodeEmoji = Enumerable.ToUnicode(emoji);
// unicodeEmoji will be "\ud83d\ude0a"

ToList

Converts a sequence to a List<T>.

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);

Example

This example converts an array to a list.

int[] numbersArray = { 1, 2, 3 };
List<int> numbersList = numbersArray.ToList();
// numbersList will be a List<int> containing { 1, 2, 3 }

ToArray

Converts a sequence to an array.

public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source);

Example

This example converts a list to an array.

List<int> numbersList = new List<int> { 1, 2, 3 };
int[] numbersArray = numbersList.ToArray();
// numbersArray will be an array containing { 1, 2, 3 }

ToDictionary

Converts a sequence to a Dictionary<TKey, TValue>.

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector);

Example

This example converts a list of people to a dictionary keyed by their name.

var people = new[] {
    new { Name = "Alice", Age = 30 },
    new { Name = "Bob", Age = 25 }
};
Dictionary<string, int> ageDictionary = people.ToDictionary(p => p.Name, p => p.Age);
// ageDictionary will contain { {"Alice", 30}, {"Bob", 25} }

Union

Produces the set union of two sequences.

public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

Example

This example finds the unique elements present in either of two lists.

List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 3, 4, 5 };
IEnumerable<int> union = list1.Union(list2);
// union will contain { 1, 2, 3, 4, 5 }

Where

Filters a sequence of values based on a predicate.

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);

Example

This example filters an array to include only even numbers.

int[] numbers = { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> evenNumbers = numbers.Where(n => n % 2 == 0);
// evenNumbers will contain { 2, 4, 6 }

Zip

Correlates the elements of two sequences by using the selector function.

public static IEnumerable<TResult> Zip<T1, T2, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, Func<T1, T2, TResult> resultSelector);

Example

This example zips two arrays together, creating pairs of elements.

string[] names = { "Alice", "Bob", "Charlie" };
int[] ages = { 30, 25, 35 };
IEnumerable<string> combined = names.Zip(ages, (name, age) => $"{name} is {age} years old.");
// combined will contain { "Alice is 30 years old.", "Bob is 25 years old.", "Charlie is 35 years old." }