preserving以便与LINQ [英] Preserving order with LINQ
问题描述
我使用LINQ到对象指令的有序阵列上。
哪些操作不应该怎么做,以确保该数组的顺序不会改变?
I use LINQ to Objects instructions on an ordered array. Which operations shouldn't I do to be sure the order of the array is not changed?
推荐答案
我检查的<一个方法href=\"http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods.aspx\">System.Linq.Enumerable,丢弃任何返回非IEnumerable的结果。我检查了每一个言论,以确定如何结果的顺序将来自源的顺序不同。
I examined the methods of System.Linq.Enumerable, discarding any that returned non-IEnumerable results. I checked the remarks of each to determine how the order of the result would differ from order of the source.
$ P $绝对pserves订单。您可以通过索引源元素映射到一个结果元素
Preserves Order Absolutely. You can map a source element by index to a result element
- AsEnumerable
- 铸造
- 的毗连
- 选择
- 的ToArray
- 了ToList
preserves订单。元件被过滤,而不是重新排序
Preserves Order. Elements are filtered, but not re-ordered.
- 鲜明
- 除了
- 相交
- OfType
- 跳过
- SkipWhile
- 以
- TakeWhile
- 其中,
- 邮编(在.NET中新4)
破阵订单 - 我们不知道会发生什么样的顺序结果
Destroys Order - we don't know what order to expect results in.
- ToDictionary
- ToLookup
重新定义订单明确 - 用这些来改变结果的顺序
Redefines Order Explicitly - use these to change the order of the result
- 排序依据
- OrderByDescending
- 反向
- ThenBy
- ThenByDescending
根据一定的规则重新定义订购。
Redefines Order according to some rules.
- GROUPBY - 的IGrouping对象在基于所产生的每个IGrouping的第一密钥在源中的元素的顺序的顺序产生。在分组的元素产生在它们出现在源中的顺序。
- 群组加入 - 群组加入preserves外的元素的顺序,以及用于外,从内的匹配元素的顺序中的每个元素 。
- 加入 - preserves外的元素的顺序,并为每个这些元件,内的匹配元素的顺序的。
- 的SelectMany - 源中的每个元素,选择被调用并返回值的序列
- 联盟 - 当此方法返回的对象枚举,列举了联盟第一和第二的顺序并产生尚未产生每个元素。
编辑:我在此基础上<感动清晰到preserving秩序href=\"https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/Enumerable.cs\">implementation.
private static IEnumerable<TSource> DistinctIterator<TSource>
(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
{
Set<TSource> set = new Set<TSource>(comparer);
foreach (TSource element in source)
if (set.Add(element)) yield return element;
}
这篇关于preserving以便与LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!