SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method DistinctUntilChanged

    | Edit this page

    DistinctUntilChanged<TSource>(IEnumerable<TSource>)

    Returns consecutive distinct elements by using the default equality comparer to compare values.

    Declaration
    public static IEnumerable<TSource> DistinctUntilChanged<TSource>(this IEnumerable<TSource> source)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence without adjacent non-distinct elements.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    This method uses deferred execution semantics and streams its results.

    Examples

    The following code example demonstrates how to get distinct elements from a sequence using DistinctUntilChanged.

    var sequence = new Item[]
    {
        new(key: 3, text: "1"),
        new(key: 3, text: "2"),
        new(key: 2, text: "3"),
        new(key: 2, text: "4"),
        new(key: 1, text: "5"),
        new(key: 1, text: "6"),
        new(key: 3, text: "7"),
        new(key: 3, text: "8"),
        new(key: 2, text: "9"),
        new(key: 2, text: "10"),
        new(key: 1, text: "11"),
        new(key: 1, text: "12"),
    };
    
    // Get distinct 
    var result = sequence.DistinctUntilChanged();
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(3, 1), (2, 3), (1, 5), (3, 7), (2, 9), (1, 11)]
    
    class Item : IEquatable<Item>
    {
        public Item(int key, string text)
        {
            Key = key;
            Text = text;
        }
    
        public int Key { get; }
        public string Text { get; }
    
        public bool Equals(Item other) =>
            this.Key == other.Key;
    
        public override int GetHashCode() =>
            this.Key.GetHashCode();
    
        public override string ToString() =>
            $"({this.Key}, {this.Text})";
    }
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Edit this page

    DistinctUntilChanged<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>?)

    Returns consecutive distinct elements by using the specified equality comparer to compare values.

    Declaration
    public static IEnumerable<TSource> DistinctUntilChanged<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource>? comparer)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    IEqualityComparer<TSource> comparer

    Comparer used to compare values.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence without adjacent non-distinct elements.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    This method uses deferred execution semantics and streams its results.

    Examples

    The following code example demonstrates how to get distinct elements from a sequence using DistinctUntilChanged.

    var sequence = new Item[]
    {
        new(key: 3, text: "1"),
        new(key: 3, text: "2"),
        new(key: 2, text: "3"),
        new(key: 2, text: "4"),
        new(key: 1, text: "5"),
        new(key: 1, text: "6"),
        new(key: 3, text: "7"),
        new(key: 3, text: "8"),
        new(key: 2, text: "9"),
        new(key: 2, text: "10"),
        new(key: 1, text: "11"),
        new(key: 1, text: "12"),
    };
    
    // Get distinct 
    var result = sequence
        .DistinctUntilChanged(
            new ItemComparer());
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(3, 1), (2, 3), (1, 5), (3, 7), (2, 9), (1, 11)]
    
    class Item
    {
        public Item(int key, string text)
        {
            Key = key;
            Text = text;
        }
    
        public int Key { get; }
        public string Text { get; }
    
        public override string ToString() =>
            $"({this.Key}, {this.Text})";
    }
    
    class ItemComparer : IEqualityComparer<Item>
    {
        public bool Equals(Item x, Item y) =>
            x.Key == y.Key;
    
        public int GetHashCode(Item obj) =>
            obj.Key.GetHashCode();
    }
    
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Edit this page

    DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

    Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.

    Declaration
    public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Func<TSource, TKey> keySelector

    Key selector.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence without adjacent non-distinct elements.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    TKey

    Key type.

    Remarks

    This method uses deferred execution semantics and streams its results.

    Examples

    The following code example demonstrates how to get distinct elements from a sequence using DistinctUntilChanged.

    var sequence = new[]
    {
        (key: 3, text: "1"),
        (key: 3, text: "2"),
        (key: 2, text: "3"),
        (key: 2, text: "4"),
        (key: 1, text: "5"),
        (key: 1, text: "6"),
        (key: 3, text: "7"),
        (key: 3, text: "8"),
        (key: 2, text: "9"),
        (key: 2, text: "10"),
        (key: 1, text: "11"),
        (key: 1, text: "12"),
    };
    
    // Get distinct 
    var result = sequence
        .DistinctUntilChanged(x => x.key);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(3, 1), (2, 3), (1, 5), (3, 7), (2, 9), (1, 11)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    | Edit this page

    DistinctUntilChanged<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)

    Returns consecutive distinct elements based on a key value by using the specified equality comparer to compare key values.

    Declaration
    public static IEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Func<TSource, TKey> keySelector

    Key selector.

    IEqualityComparer<TKey> comparer

    Comparer used to compare key values.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence without adjacent non-distinct elements.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    TKey

    Key type.

    Remarks

    This method uses deferred execution semantics and streams its results.

    Examples

    The following code example demonstrates how to get distinct elements from a sequence using DistinctUntilChanged.

    var sequence = new[]
    {
        (key: "aa", text: "1"),
        (key: "Aa", text: "2"),
        (key: "AA", text: "3"),
        (key: "BB", text: "4"),
        (key: "bB", text: "5"),
        (key: "Cc", text: "6"),
        (key: "CC", text: "7"),
        (key: "Aa", text: "8"),
        (key: "aA", text: "9"),
        (key: "bb", text: "10"),
        (key: "bB", text: "11"),
        (key: "CC", text: "12"),
    };
    
    // Get distinct 
    var result = sequence
        .DistinctUntilChanged(
            x => x.key,
            StringComparer.OrdinalIgnoreCase);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(aa, 1), (BB, 4), (Cc, 6), (Aa, 8), (bb, 10), (CC, 12)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    © SuperLinq Authors. All rights reserved.