[英]Return the highest value in the IEnumerable<T> list collection each step using OrderByDescending
这是我第一次在网站上提问,所以我可能做得不对。 我需要在我的 IEnumerable 列表中查找并返回最高值,但不能显示任何重复项。 我也不能使用任何副本或临时 collections,或使用不同的。 必须首先通过获取列表并执行 OrderByDescending 来完成。
这是我到目前为止所拥有的:
public static IEnumerable<T> PullMax<T> (IEnumerable<T> sourcelist) where T : IComparable
{
IEnumerable<T> list = sourcelist.OrderByDescending(item => item);
foreach (T item in list)
{
yield return item;
}
}
这是我从这段代码中得到的结果:
foreach (int item in Util.PullMax(Util.GenRange(iMin, iMax).Concat(Util.GenRange(iMin + 5, iMax + 5))))
{
Console.Write($"\nMaximum Value: {item}");
}
Maximum Value: 15
Maximum Value: 14
Maximum Value: 13
Maximum Value: 12
Maximum Value: 11
Maximum Value: 10
Maximum Value: 10
Maximum Value: 9
Maximum Value: 9
Maximum Value: 8
Maximum Value: 8
Maximum Value: 7
Maximum Value: 6
Maximum Value: 5
Maximum Value: 4
Maximum Value: 3
这应该是预期的结果:
Maximum Value: 15
Maximum Value: 14
Maximum Value: 13
Maximum Value: 12
Maximum Value: 11
Maximum Value: 10
Maximum Value: 9
Maximum Value: 8
Maximum Value: 7
Maximum Value: 6
Maximum Value: 5
Maximum Value: 4
Maximum Value: 3
我被要求展示 Util.GenRange()
public static IEnumerable<int> GenRange(int iMin, int iMax)
{
while (true)
{
for (int i = iMin; i < iMax; i++)
{
yield return i;
}
yield break;
}
}
我得到的结果有重复,我不能拥有它们。 如何在没有任何副本或临时 collections 甚至使用不同的情况下摆脱它们?
一种避免与另一个解决方案的多重枚举问题的方法(即另一个解决方案在可枚举对象上迭代不止一次):
public static IEnumerable<T> PullMax<T> (IEnumerable<T> sourcelist) where T : IComparable
{
IEnumerable<T> list = sourcelist.OrderByDescending(item => item);
T previousValue = default(T);
bool firstIteration = true;
foreach (T item in list)
{
if (firstIteration)
firstIteration = false;
else if (item.CompareTo(previousValue) == 0)
continue;
previousValue = item;
yield return item;
}
}
基本上它总是返回第一个元素 - 然后对于第一个元素以外的元素,只有当它们与前一个元素不匹配时才返回它们。 这对于某些类型的数据(例如 sourcelist 是IQueryable
)将大大加快,因为它避免了不必要的计数等。
自己编写这个算法有很多不同的选择。
但是,在可能的情况下重用代码总是更好,唯一的问题是您还没有找到仅通过一次执行不同排序的 LINQ 方法。
但是有SortedSet<T>
,它基本上是一个排序的不同列表,它要求O(n log n)
进行插入,并且可以采用IEnumerable
。 它不会抛出重复项,它只是忽略它们:
static IEnumerable<T> PullMax<T>(this IEnumerable<T> source, IComparer<T> comparer)
{
return new SortedSet<T>(source, comparer);
}
要将其按降序排列到 go,请使用-
翻转 lambda 中的比较结果:
list.PullMax((a, b) => -a.CompareTo(b))
你写了:
我需要在我的 IEnumerable 列表中查找并返回最高值,但不能显示任何重复项。
从您的代码中,我了解到您不仅想要输入序列的最高值,还想要按降序排列的值,没有任何重复。
为了使使用类似于 LINQ,我将其创建为扩展方法。 如果您不熟悉扩展方法,请阅读扩展方法揭秘
扩展方法只是语法糖。 它使读者看起来好像您调用的方法是 class 的方法。
而不是在 static class 中调用方法:
var customers = FetchCustomers();
var maxCustomers = ExtraCustomerFunctions.PullMax(customers);
你可以写:
var customers = FetchCustomers();
var maxCustomers = customers.PullMax();
为此,您唯一需要做的就是在 static class 中创建一个 static 方法并在第一个参数之前使用this
词。
回到你的问题
要求
从实现IComparable<T>
的 class T
对象的输入序列中,返回一个序列,其中第一个元素是最大的项目,然后是最大的但一个,等等。丢弃重复值。
public class MyExtensionMethods
{
public static IEnumerable<T> PullMax<T> (this IEnumerable<T> source)
where T : IComparable<T>
{
IEnumerable<T> orderedSource = source.OrderByDescending(item => item);
using (IEnumerator<T> enumerator = orderedSource.GetEnumerator())
{
if (enumerator.MoveNext())
{
// The sequence is not empty.
// return the first item, and remember that you returned this value
T lastReturnedItem = enumerator.Current;
yield return lastReturnedItem;
// enumerate the rest, skip the items with a value that you just returned
while (enumerator.MoveNext())
{
// Current is either as large as the last returned one, or it is smaller
if (enumerator.Curren.Compare(lastReturnedItem) < 0)
{
// Current is smaller; return it, and remember that you returned it
lastReturnedItem = enumerator.Current;
yield return lastReturnedItem;
}
}
}
}
}
}
这似乎有很多代码,但其中大部分是注释。
用法:
int[] numbers = FetchNumbers();
var pulledMaxNumbers = numbers.PullMax();
如果您打算重用此方法,请考虑使其更通用、更类似于 LINQ。 这样,对于不可比较的项目,该方法将很容易使用。
客户没有可比性,但您可以在生日或邮政编码上进行比较。 PullMax 客户只需稍作改动即可:
// PullMax customers, oldest Customers first:
IEnumerable<Customer> customers = this.FetchCustomers();
var result = customers.PullMax(customer => customer.BirthDay);
为此,请创建一个带有属性选择器和通用 IComparer 的方法。 像这样的东西:
static IEnumerable<T> PullMax<T>(this IEnumerable<T> source)
{
return source.PullMax(item => item);
}
static IEnumerable<T> PullMax<T, TKey>(this IEnumerable<T> source,
Func<T, TKey> propertySelector)
{
return source.PullMax(propertySelector, null);
}
static IEnumerable<T> PullMax<T, TKey>(this IEnumerable<T> source,
Func<T, TKey> keySelector,
IComparer<TKey> comparer)
{
// TODO: check source and keySelector not null
if (comparer == null) comparer = Comparer<TKey>.Default;
// rest of code is similar to code above, difference: check on keySelector
IEnumerable<T> orderedSource = source.OrderByDescending(keySelector);
using (IEnumerator<T> enumerator = orderedSource.GetEnumerator())
{
if (enumerator.MoveNext())
{
T currentValue = enumerator.Current;
yield return currentValue
TKey lastKeyValue = keySelector(currentValue);
while (enumerator.MoveNext())
{
currentValue = enumerator.Current;
TKey keyValue = keySelector(currentValue);
if (comparer.Compare(keyValue, lastKeyValue) < 0)
{
yield return currentValue;
lastKeyValue = keyValue;
}
等等
您甚至可以将 PullMax 放入 LINQ 串联中:
var result = Customers.Where(customer => customer.City == "New York")
.PullMax(customer => customer.PostCode)
.Select(customer => new
{
Id = customer.Id,
Name = customer.Name,
});
那么完全没有任何临时变量或不同的就没有机会了。 但最便宜的解决方法是使用一个临时变量来保存最后一个返回值,并检查该值是否相同。
也许这种方法应该可以解决问题
public static IEnumerable<T> PullMax<T>(IEnumerable<T> sourcelist) where T : IComparable
{
IEnumerable<T> list = sourcelist.OrderByDescending(item => item);
if (sourcelist.LongCount() > 0) yield return list.ElementAt(0);
T tmp = sourcelist.ElementAt(0);
foreach (T item in list)
{
if (item.CompareTo(tmp) < 1) continue;
tmp = item;
yield return item;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.