猿问

子数组上的 C# LINQ OrderBy

使用 C# LINQ,我们是否可以仅对 List 的一系列元素进行 OrderBy 而保留其他元素?

例如,输入列表是 {"a","b","c","d","e"},想象中的 OrderByDescending 是这样的:

OrderByDescending(delegate d,int start_index,int end_index)
l=l.OrderByDescending(x=>x,1,3).ToList();

结果是:{"a","d","c","b","e"}

没有这个功能,我需要拆分/LINQ Orderby/rejoin,这会失去 LINQ 的精神。


斯蒂芬大帝
浏览 120回答 2
2回答

婷婷同学_

您可以使用Skip并Take实现它。var input = new[] { "a", "b", "c", "d", "e" };var res = input.Take(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Concat(input.Skip(1).Take(3).OrderByDescending(e => e))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Concat(input.Skip(4));你也可以像这样做一个扩展方法public static class IEnumerableExt{&nbsp; &nbsp; public static IEnumerable<TSource> OrderRangeByDescending<TSource, TKey>(this IEnumerable<TSource> input, Func<TSource, TKey> keySelector, int from, int length)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return input.Take(from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Concat(input.Skip(from).Take(length).OrderByDescending(keySelector))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Concat(input.Skip(from + length));&nbsp; &nbsp; }&nbsp; &nbsp; public static IEnumerable<TSource> OrderRangeBy<TSource, TKey>(this IEnumerable<TSource> input, Func<TSource, TKey> keySelector, int from, int length)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return input.Take(from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Concat(input.Skip(from).Take(length).OrderBy(keySelector))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Concat(input.Skip(from + length));&nbsp; &nbsp; }}var input = new[] { "a", "b", "c", "d", "e" };var res = input.OrderRangeByDescending(e => e, 1, 3);

呼唤远方

重复调用Skip和Take可能会对性能产生影响,特别是如果源是由密集计算生成的。最佳解决方案将要求对源的读取仅发生一次。这可以通过将源拆分为多个枚举但使用单个枚举器来实现。拥有这样一个 Splitter 将使我们能够很容易地实现OrderRangeBy/OrderRangeByDescending方法:public static IEnumerable<TSource> OrderRangeBy<TSource, TKey>(&nbsp; &nbsp; this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,&nbsp; &nbsp; int startIndex, int endIndexExclusive){&nbsp; &nbsp; var parts = source.Split(startIndex, endIndexExclusive);&nbsp; &nbsp; return parts[0].Concat(parts[1].OrderBy(keySelector)).Concat(parts[2]);}public static IEnumerable<TSource> OrderRangeByDescending<TSource, TKey>(&nbsp; &nbsp; this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,&nbsp; &nbsp; int startIndex, int endIndexExclusive){&nbsp; &nbsp; var parts = source.Split(startIndex, endIndexExclusive);&nbsp; &nbsp; return parts[0].Concat(parts[1].OrderByDescending(keySelector)).Concat(parts[2]);}下面是 Splitter 的一个实现:public static IEnumerable<TSource>[] Split<TSource>(&nbsp; &nbsp; this IEnumerable<TSource> source, params int[] indices){&nbsp; &nbsp; var parts = new IEnumerable<TSource>[indices.Length + 1];&nbsp; &nbsp; var enumerator = source.GetEnumerator();&nbsp; &nbsp; int index = 0;&nbsp; &nbsp; for (int i = 0; i < indices.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; parts[i] = GetPart(indices[i]);&nbsp; &nbsp; }&nbsp; &nbsp; parts[indices.Length] = GetPart(Int32.MaxValue);&nbsp; &nbsp; return parts;&nbsp; &nbsp; IEnumerable<TSource> GetPart(int maxIndexExclusive)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (index >= maxIndexExclusive) goto finish;&nbsp; &nbsp; &nbsp; &nbsp; while (enumerator.MoveNext())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return enumerator.Current;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index >= maxIndexExclusive) break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finish: if (maxIndexExclusive == Int32.MaxValue) enumerator.Dispose();&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答