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.