We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow us to know the index of the elements inside their groups created by a keySelecton function.
keySelecton
static IEnumerable<TResult> IndexBy<T, TKey, TResult>(this IEnumerable<T> source, Func<T, TKey> keySelector, Func<T, TKey, int, TResult> resultSelector) { return source .Select((element, index) => new { element, index }) .GroupBy(pair => keySelector(pair.element)) .SelectMany(grup => grup.Select((pair, innerIndex) => new { pair, grup.Key, innerIndex })) .OrderBy(x => x.pair.index) .Select(x => resultSelector(x.pair.element, x.Key, x.innerIndex)); }
var nomes = new[] { "ana", "beatriz", "carla", "bob", "davi", "adriano", "angelo", "carla", }; var resultado = nomes.IndexBy(x => x.First(), (nome, key, index) => new { nome, key, index });
outputs
{ nome = ana, key = a, index = 0 } { nome = beatriz, key = b, index = 0 } { nome = carla, key = c, index = 0 } { nome = bob, key = b, index = 1 } { nome = davi, key = d, index = 0 } { nome = adriano, key = a, index = 1 } { nome = angelo, key = a, index = 2 } { nome = carla, key = c, index = 1 }
The text was updated successfully, but these errors were encountered:
This is interesting and I would welcome a PR!
Sorry, something went wrong.
5205ea2
leandromoh
No branches or pull requests
Allow us to know the index of the elements inside their groups created by a
keySelecton
function.Prototype
Example
outputs
The text was updated successfully, but these errors were encountered: