This HowTo introduces the role of Comparers in a LINQ statement
Introduction
LINQ provides various operators that are used to evaluate the elements in a set of items. The elements are compared with each other for this evaluation. A comparer can be used to influence the comparison.
Scenario 1
I – Input | P – Processing | O – Output |
---|---|---|
{"FR", "IT", "ES" } |
check if “ES” is contained at least 1 time | True |
{"FR", "IT", "ES" } |
check if “es” is contained at least 1 time | False |
To do this, the elements of the set with items are compared with the value to be checked for equality and the result to be returned is determined:
- True - “ES” is contained
- False - “es” is not contained
Scenario 2
To serve other scenarios, a different understanding of element equality might be necessary
I – Input | P – Processing | O – Output |
---|---|---|
{"FR", "IT", "ES" } |
check if “ES” is contained at least 1 time | True |
{"FR", "IT", "ES" } |
check if ->“es”<- is contained at least 1 time | → True ← |
In this scenario, the elements are compared case insensitive with the comparison value and the result to be returned is determined:
- True - “ES” is contained
- True - “es” is contained
The definition of how elements are to be regarded as the same therefore determines how the return result is to be calculated.
Comparers in LINQ
To control how items are compared to each other, LINQ allows the use of a specific comparer.
The example above shows that the default comparer does not work on a case-insensitive mode.
I – Input | P – Processing | O – Output |
---|---|---|
{"FR", "IT", "ES" } |
check if “ES” is contained at least 1 time | True |
{"FR", "IT", "ES" } |
check if ->“es”<- is contained at least 1 time | → True ← |
By specifying a specialized comparator, a case-insensitive comparison can be achieved. This makes the scenario easy to implement
I – Input | P – Processing | O – Output |
---|---|---|
{FR, IT, ES } | check if “ES” is contained at least 1 time | True |
{FR, IT, ES } | check if ->“es”<- is contained at least 1 time | → True ← |
In short, the comparison, which should consider elements to be the same regardless of their upper or lower case, was controlled by specifying a specific comparer.
More on Comparers
In general, comparers can be divided into the following categories
- Default Comparers
- Specialized Comparers
- Custom Comparers
Default Compares
Comparers that offer the comparison for a specific data type and are already available in .Net
Example: DataRowComparer.Default
- for the comparison of data rows of DataTables
Specialized Comparers
Comparers that offer a certain comparison logic
Example: StringComparer.InvariantCultureIgnoreCase
Culture invariant case insensitivity of string values
Custom Comparers
Comparers that have implemented their own comparison logic
Comparison types
When elements are compared, they are evaluated. The evaluation depends on what is to be checked. Examples of evaluations
- Equality, e.g. a = b
- Sorting, e.g. a < b
Accordingly, some LINQ Operators provide the option of optional specifying a specific comparer.
LINQ Operators, optional using comparers
Operator | Link |
---|---|
Contains | [HowTo] LINQ - First Start # Contains Operator |
Intersect, IntersectBy | |
Except, ExceptBy | |
Intersect, IntersectBy | |
Union, UnionBy | |
Distinct, DistinctBy | |
Order Operators | |
GroupBy | |
SequenceEqual |
Samples
Contains Operator on a string list
Intersect Operator on DataTables
References
Documentation
- IComparer<T> Interface (System.Collections.Generic) | Microsoft Learn
- IEqualityComparer<T> Interface (System.Collections.Generic) | Microsoft Learn
- StringComparer Class (System) | Microsoft Learn
- DataRowComparer Class (System.Data) | Microsoft Learn
Questions
For questions on your case open a new topic and get individual support