Код: Выделить всё
public static IEnumerable DistinctLastBy(
this IEnumerable source,
Func keySelector,
IEqualityComparer comparer = default)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(keySelector);
return source
.GroupBy(keySelector, comparer)
.Select(g => g.Last());
}
Код: Выделить всё
string[] source = ["giraffe", "Elephant", "Cat", "Eagle", "Gazelle", "Cow", "chicken"];
IEnumerable distinct = source
.DistinctLastBy(x => x.Substring(0, 1), StringComparer.OrdinalIgnoreCase);
Console.WriteLine(String.Join(", ", distinct));
Код: Выделить всё
Eagle, Gazelle, chicken
Код: Выделить всё
Gazelle, Eagle, chicken
Интернет-демо.
Как исправить оператор DistinctLastBy, чтобы он выдавал правильный результат?
Я искал дубликат и я ничего не нашел.
Подробнее здесь: https://stackoverflow.com/questions/798 ... duplicates
Мобильная версия