enkoni's People
enkoni's Issues
Extension method request for grouping by a key as long as the key doesn't change
Say we have a list of components:
List<Component> components = new List<Component>
{
new Component {Region = "One", ComponentId = 1},
new Component {Region = "One", ComponentId = 2},
new Component {Region = "Two", ComponentId = 3},
new Component {Region = "One", ComponentId = 4},
};
I'd like to group them so that the first two, having Region "One" form a group, then a group "Two" and finally yet another group for "One". So group them as long as the key doesn't change.
regions = components.GroupByAdjacent(c => c.Region)
I already an implementation that does this, maybe you can use it.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace LinqExtensions
{
internal class Grouping<TElement, TKey> : IEnumerable<TElement>, IGrouping<TKey, TElement>
{
private List<TElement> GroupList { get; set; }
public TKey Key { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<TElement>) this).GetEnumerator();
}
IEnumerator<TElement> IEnumerable<TElement>.GetEnumerator()
{
return ((IEnumerable<TElement>) GroupList).GetEnumerator();
}
public Grouping(List<TElement> source, TKey key)
{
Key = key;
GroupList = source;
}
}
public static class LocalExtensions
{
public static IEnumerable<IGrouping<TKey, TSource>> GroupByAdjacent<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
if (source == null) throw new ArgumentNullException("source");
IEnumerator<TSource> enumerator = source.GetEnumerator();
if (!enumerator.MoveNext()) yield break;
EqualityComparer<TKey> comparer = EqualityComparer<TKey>.Default;
List<TSource> elementsSoFar = new List<TSource>();
TKey lastKey = keySelector(enumerator.Current);
do
{
TKey newKey = keySelector(enumerator.Current);
if (!comparer.Equals(lastKey, newKey))
{
yield return new Grouping<TSource, TKey>(elementsSoFar, lastKey);
lastKey = newKey;
elementsSoFar = new List<TSource>();
}
elementsSoFar.Add(enumerator.Current);
} while (enumerator.MoveNext());
yield return new Grouping<TSource, TKey>(elementsSoFar, lastKey);
}
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.