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# Queryable SequenceEqual() Method
The SequenceEqual() method in C# is used to determine whether two sequences are equal by comparing elements in the same position. This method returns true if both sequences contain the same elements in the same order, otherwise it returns false.
The method is part of the System.Linq namespace and can be used with IQueryable collections to perform element-by-element comparison.
Syntax
Following is the syntax for the SequenceEqual() method −
public static bool SequenceEqual<TSource>(
this IQueryable<TSource> source1,
IEnumerable<TSource> source2
)
With custom comparer −
public static bool SequenceEqual<TSource>(
this IQueryable<TSource> source1,
IEnumerable<TSource> source2,
IEqualityComparer<TSource> comparer
)
Parameters
source1 − The first sequence to compare.
source2 − The second sequence to compare.
comparer − An optional equality comparer to compare elements.
Return Value
The method returns a bool value −
true − If the sequences are equal in length and corresponding elements are equal.
false − If the sequences differ in length or any corresponding elements are not equal.
Using SequenceEqual() with Custom Objects
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Employee emp1 = new Employee { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 };
Employee emp2 = new Employee { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 };
List<Employee> employee1 = new List<Employee> { emp1, emp2 };
List<Employee> employee2 = new List<Employee> { emp1, emp2 };
bool res = employee1.AsQueryable().SequenceEqual(employee2);
Console.WriteLine("Lists are equal? = " + res);
// Different order - should return false
List<Employee> employee3 = new List<Employee> { emp2, emp1 };
bool res2 = employee1.AsQueryable().SequenceEqual(employee3);
Console.WriteLine("Different order equal? = " + res2);
}
}
public class Employee {
public int EmployeeRank { get; set; }
public string EmpName { get; set; }
public int EmpMarks { get; set; }
}
The output of the above code is −
Lists are equal? = True Different order equal? = False
Using SequenceEqual() with Primitive Types
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> numbers1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> numbers2 = new List<int> { 1, 2, 3, 4, 5 };
List<int> numbers3 = new List<int> { 1, 2, 3, 4, 6 };
bool equal1 = numbers1.AsQueryable().SequenceEqual(numbers2);
bool equal2 = numbers1.AsQueryable().SequenceEqual(numbers3);
Console.WriteLine("numbers1 equals numbers2: " + equal1);
Console.WriteLine("numbers1 equals numbers3: " + equal2);
// Different lengths
List<int> numbers4 = new List<int> { 1, 2, 3 };
bool equal3 = numbers1.AsQueryable().SequenceEqual(numbers4);
Console.WriteLine("Different lengths equal: " + equal3);
}
}
The output of the above code is −
numbers1 equals numbers2: True numbers1 equals numbers3: False Different lengths equal: False
Using SequenceEqual() with Custom Comparer
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> words1 = new List<string> { "Hello", "World" };
List<string> words2 = new List<string> { "HELLO", "WORLD" };
// Default comparison (case-sensitive)
bool equal1 = words1.AsQueryable().SequenceEqual(words2);
Console.WriteLine("Case-sensitive comparison: " + equal1);
// Case-insensitive comparison
bool equal2 = words1.AsQueryable().SequenceEqual(words2, StringComparer.OrdinalIgnoreCase);
Console.WriteLine("Case-insensitive comparison: " + equal2);
}
}
The output of the above code is −
Case-sensitive comparison: False Case-insensitive comparison: True
Key Rules
Both sequences must have the same number of elements to be considered equal.
Elements are compared in the same positional order − [1,2,3] is not equal to [3,2,1].
For reference types, the method uses reference equality by default unless overridden.
Use a custom
IEqualityComparerfor specialized comparison logic.
Conclusion
The SequenceEqual() method provides an efficient way to compare two sequences element by element in the same order. It supports both default equality comparison and custom comparers, making it versatile for different comparison scenarios in LINQ operations.
