Merge elements in list by property

Chostakovitch

Context

I have a list of time intervals. Time interval type is HistoMesures.

Each HistoMesure is defined by a Debut (begin) property, a Fin (end) property, and a Commentaires (a little note) property.

My list is made in such a way that :

  • All HistoMesure are exclusive, I mean that they can't be overlapping each other.
  • The list is sorted by Debut, so by the beggining of the interval.
  • Edit : All HistoMesure are contiguous in this configuration.

Question

I want to merge (transform two little intervals in one big interval) all adjacent HistoMesure which have the same Commentaires. Currently I achieve this that way :

//sortedHistos type is List<HistoMesure>
int i = 0;
while (i < sortedHistos.Count - 1)
{
    if (sortedHistos[i].Commentaires == sortedHistos[i + 1].Commentaires)
    {
        sortedHistos[i].Fin = sortedHistos[i + 1].Fin;
        sortedHistos.RemoveAt(i + 1);
    }
    else
    {
        ++i;
    }
}

But I feel that it exists a more elegant way to do this, maybe with LINQ. Do you have any suggestion ?

DavidG

Using Linq and borrowing from this article to group by adjacent values, this should work:

Your query:

var filteredHistos = sortedHistos
    .GroupAdjacent(h => h.Commentaires)
    .Select(g => new HistoMesure
    {
        Debut = g.First().Debut,
        Fin = g.Last().Fin,
        Commentaires = g.Key
    });

And copying from the article, the rest of the code to group by:

public class GroupOfAdjacent<TSource, TKey> : IEnumerable<TSource>, IGrouping<TKey, TSource>
{
    public TKey Key { get; set; }
    private List<TSource> GroupList { get; set; }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return ((System.Collections.Generic.IEnumerable<TSource>)this).GetEnumerator();
    }
    System.Collections.Generic.IEnumerator<TSource> System.Collections.Generic.IEnumerable<TSource>.GetEnumerator()
    {
        foreach (var s in GroupList)
            yield return s;
    }
    public GroupOfAdjacent(List<TSource> source, TKey key)
    {
        GroupList = source;
        Key = key;
    }
}
public static class LocalExtensions
{
    public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>(
        this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector)
    {
        TKey last = default(TKey);
        bool haveLast = false;
        List<TSource> list = new List<TSource>();
        foreach (TSource s in source)
        {
            TKey k = keySelector(s);
            if (haveLast)
            {
                if (!k.Equals(last))
                {
                    yield return new GroupOfAdjacent<TSource, TKey>(list, last);
                    list = new List<TSource>();
                    list.Add(s);
                    last = k;
                }
                else
                {
                    list.Add(s);
                    last = k;
                }
            }
            else
            {
                list.Add(s);
                last = k;
                haveLast = true;
            }
        }
        if (haveLast)
            yield return new GroupOfAdjacent<TSource, TKey>(list, last);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Merge elements in list by property

From Dev

Group and Merge elements in same list

From Dev

Combine/merge lists by elements names (list in list)

From Dev

Python Merge List With Common Elements in the List

From Dev

JAXB and property ordering with list of elements

From Dev

Merge items of list based on common property

From Dev

R: merge elements of a list according to a vector of factors

From Dev

C++ Merge List Elements in Pairs

From Dev

R: How to merge logical elements in a list

From Dev

merge two list of elements returned by querySelectorAll

From Dev

How to merge names with common elements in list?

From Dev

Merge 2 consecutive elements in a List<string>

From Dev

Merge elements in a list, if they are in a certain unicode range

From Dev

How to merge two elements in a list in Python

From Dev

How to merge elements within the same list?

From Dev

Combine/merge lists by elements names (list in list in list)

From Dev

Merge the first elements in a list a tuples if the second elements are the same?

From Dev

Get n elements of list having the highest property

From Dev

Get n elements of list having the highest property

From Dev

Append and delete elements from a list using property

From Dev

Cross merge list elements to get a list of tuples in Python

From Dev

The fast way to select the elements in a list which a property is in another list

From Dev

JavaFX: Bind a list to a member property of elements of another list

From Dev

JavaFX: Bind a list to a member property of elements of another list

From Dev

How to merge overlapping integer vector elements of a list in R

From Dev

How to merge elements of dataframes of different sizes containded in a list [R]?

From Dev

Using Pandas to merge 2 list of dicts with common elements

From Dev

How to merge two lists? Preserving identical list elements for set manipulation

From Dev

Merge two lists into a dictionary and sum over the elements of the second list

Related Related

  1. 1

    Merge elements in list by property

  2. 2

    Group and Merge elements in same list

  3. 3

    Combine/merge lists by elements names (list in list)

  4. 4

    Python Merge List With Common Elements in the List

  5. 5

    JAXB and property ordering with list of elements

  6. 6

    Merge items of list based on common property

  7. 7

    R: merge elements of a list according to a vector of factors

  8. 8

    C++ Merge List Elements in Pairs

  9. 9

    R: How to merge logical elements in a list

  10. 10

    merge two list of elements returned by querySelectorAll

  11. 11

    How to merge names with common elements in list?

  12. 12

    Merge 2 consecutive elements in a List<string>

  13. 13

    Merge elements in a list, if they are in a certain unicode range

  14. 14

    How to merge two elements in a list in Python

  15. 15

    How to merge elements within the same list?

  16. 16

    Combine/merge lists by elements names (list in list in list)

  17. 17

    Merge the first elements in a list a tuples if the second elements are the same?

  18. 18

    Get n elements of list having the highest property

  19. 19

    Get n elements of list having the highest property

  20. 20

    Append and delete elements from a list using property

  21. 21

    Cross merge list elements to get a list of tuples in Python

  22. 22

    The fast way to select the elements in a list which a property is in another list

  23. 23

    JavaFX: Bind a list to a member property of elements of another list

  24. 24

    JavaFX: Bind a list to a member property of elements of another list

  25. 25

    How to merge overlapping integer vector elements of a list in R

  26. 26

    How to merge elements of dataframes of different sizes containded in a list [R]?

  27. 27

    Using Pandas to merge 2 list of dicts with common elements

  28. 28

    How to merge two lists? Preserving identical list elements for set manipulation

  29. 29

    Merge two lists into a dictionary and sum over the elements of the second list

HotTag

Archive