Split items to a different group each time a value changes with Linq

I want to split the items using Linq in such way that each time a command type changes the previous items go to a separate group.

I tried to split using GroupBy but it just puts all items with same Command to the same bin and disregards the order.

var groups =  data
    .GroupBy(step => step.Command)
    .Select(g => new KeyValuePair<Command, IEnumerable<MyClass>>(g.Key, g.ToList()))
    .ToList();

Data

var data = new List<MyClass>
{
    new MyClass() { Index = 0, Command = Command.M },
    new MyClass() { Index = 0, Command = Command.M },
    new MyClass() { Index = 0, Command = Command.M },
    new MyClass() { Index = 1, Command = Command.M },
    new MyClass() { Index = 1, Command = Command.M },
    new MyClass() { Index = 1, Command = Command.RC },
    new MyClass() { Index = 1, Command = Command.RC },
    new MyClass() { Index = 2, Command = Command.RC },
    new MyClass() { Index = 2, Command = Command.M },
    new MyClass() { Index = 2, Command = Command.M },
    new MyClass() { Index = 2, Command = Command.RC },
    new MyClass() { Index = 2, Command = Command.M },
    new MyClass() { Index = 3, Command = Command.M },
    new MyClass() { Index = 4, Command = Command.RC },
};

Actual Output

Group:
  Index: 0, Command: M
  Index: 0, Command: M
  Index: 0, Command: M
  Index: 1, Command: M
  Index: 1, Command: M
  Index: 2, Command: M
  Index: 2, Command: M
  Index: 2, Command: M
  Index: 3, Command: M
Group:
  Index: 1, Command: RC
  Index: 1, Command: RC
  Index: 2, Command: RC
  Index: 2, Command: RC
  Index: 4, Command: RC

Desired Output

Group:
  Index: 0, Command: M
  Index: 0, Command: M
  Index: 0, Command: M
  Index: 1, Command: M
  Index: 1, Command: M
Group:
  Index: 1, Command: RC
  Index: 1, Command: RC
  Index: 2, Command: RC
Group:
  Index: 2, Command: M
  Index: 2, Command: M
Group:
  Index: 2, Command: RC
Group:
  Index: 2, Command: M
  Index: 3, Command: M
Group:
  Index: 4, Command: RC

Classes

public enum Command
{
    M,
    RC
}
public class MyClass
{
    public Command Command { get; set; }
    public int Index { get; set; }
}

4

The MoreLinq .GroupAdjacent() function looks to be an appropriate off-the-shelf solution.

Usage:

var groups =  data
    .GroupAdjacent(step => step.Command)
    .ToList();

If you specifically need a list of ValuePair<> objects, you can use an overload that accepts a resultSelector parameter.

var groups =  data
    .GroupAdjacent(
         step => step.Command,
         (key, members) => new KeyValuePair(key, members)
    )
    .ToList();

It seems to me, that you want to enumerate your input sequence, put all elements in a group, and start a new group as soon as the value of property Command changes. All next elements will be put in the new group until Command changes again, etc. until you reach the end of your sequence.

I think the easiest method would be to extend LINQ with an extension method for IEnumerable<MyClass>, or if you want to go generic, do this for IEnumerable<T>.

If you are not familiar with extension methods, consider to read Extension Methods Demystified.

Solution only suitable for a sequence of MyClass objects

The following method takes a sequence of MyClass objects, and returns them as a sequence of ICollection of MyClass objects, according to the definition given above.

static IEnumerable<ICollection<MyClass>> ToCollections<MyClass>(
    this IEnumerable<MyClass> source)
{
    // TODO: exception if source == null?

    using (Enumerator<MyClass> enumerator = source.GetEnumerator())
    {
        // as long as source has elements, process them
        bool elementAvailable = enumerator.MoveNext()
        while (elementAvailable);
        {
            // start of a new Collection, remember the common Command value:
            Command currentCommand = enumerator.Current.Command;
            ICollection<MyClass> collection = new List<MyClass>();
            collection.Add(enumerator.Current);

            // enumerate and add the objects until command changes or end of sequence
            while (elementAvailable = enumerator.MoveNext()
               && enumerator.Current.Command == currentCommand)
            {
                collection.Add(enumerator.Current);
            }

            // if here, either there are no more elements, or command changed
            // in both cases, the current collection is completed, return it
            yield return collection;
        }
        // end outer while. If there are still elements, a new collection will start
    }
    // end using enumerator
}

Usage:

List<MyClass> mySource = ...
IEnumerable<ICollection<MyClass>> collections = mySource.ToCollections();

// process the result:
forech(var collection in collections)
{
     Console.WriteLine("Group. Size {0}", collection.Count)
     foreach (MyClass member in collection)
     {
         Console.WriteLine("Index: {0}; Command: {1}", member.Index, member.Command);
     }
}

Generic solution

Input: a sequence of objects of class T and a keySelector that will define which property to watch until changed.

IEnumerable<ICollection<TSource>> ToCollections<TSource, TKey>(
    this IEnumerable<T> source,
    Func<TSource,TKey> keySelector)
{
    return ToCollections(source, keySelector, null, null)
}

Just like a lot of LINQ methods, this one calls another one with extra parameters, the one with a parameter resultSelector and an equality compater.

IEnumerable<TResult> ToCollections<TSource, TKey, TResult>(
    this IEnumerable<T> source,
    Func<TSource,TKey> keySelector,
    Func<TKey key, IEnumerable<TSource>,TResult> resultSelector,
    IEqualityComparer<TKey> comparer)
{
    // TODO: exception if source and keySelector are null
    // if resultSelector null, use the complete source object:
    if (resultSelector == null) resultSelector = tElement => tElement;
    // if comparer null, use the default comparer
    if (comparer == null) comparer = EqualityComparer<TKey>.Default;

    // The code is like above, only different types, and using the extra parameters
    using (Enumerator<TSource> enumerator = source.GetEnumerator())
    {
        // as long as source has elements, process them
        bool elementAvailable = enumerator.MoveNext()
        while (elementAvailable);
        {
            // start of a new Collection, remember the common Command value:
            TSource element = enumerator.Current;
            TKey currentKey = keySelector(element);
            ICollection<TSource> collection = new List<TSource>();
            collection.Add(element));

            // enumerate and add the elements until command changes or end of sequence
            while (elementAvailable = enumerator.MoveNext()
               && comparer.Equals(keySelector(enumerator.Current, currentKey))
            {
                // key not changed
                collection.Add(enumerator.Current);
            }

            // if here, either there are no more elements, or command changed
            // in both cases, the current collection is completed.
            // convert this collection into a result, and return it
            yield return resultSelector(currentKey, collection);
        }
        // end outer while. If there are still elements, a new collection will start
    }
    // end using enumerator
}

Usage is similar as before:

List<MyClass> mySource = ...
IEnumerable<ICollection<MyClass>> collections = mySource.ToCollections(
    element => element.Command);

Parameter keySelector says: from every MyClass object in the mySource sequence take the value of property Command as keySelector.

The nice thing is, that now you can specify the result. Suppose you don’t want to repeat the same value of Command in one collection over and over again, but something like:

Command:  M; Indexes: {0, 0, 0, 1, 1}
Command: RC; Indexes: {1, 1, 2}
Command:  M; Indexes: {2, 2}
Command: RC; Indexes: {2}
etc

Then you can do this as follows:

IEnumerable<MyClass> mySource = ...

var result = mySource.ToCollections(

    // parameter keySelector: add to collection as long as Command not changed
    element => element.Command,

    // parameter resultSelector: from every combination of Key and Collection 
    // of myClass objects until key changes,
    // make one object like new {Command ...; Indexes{ ...}}
    (key, elementsUntilKeyChanges) => new
    {
        Command = key,
        Indexes = elementsUntilKeyChanges.Select(element => element.Index),
    });

Note: I left out parameter comparer, assuming that you created an overload without a comparer that calls the overload with comparer.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật