使用LINQ保留订单 [英] Preserving order with LINQ

查看:97
本文介绍了使用LINQ保留订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ to Objects有序数组的指令。
哪些操作不应该做,以确保数组的顺序没有改变?

解决方案

我检查过 System.Linq.Enumerable 的方法,丢弃任何返回非IEnumerable结果。我检查了每个人的言论,以确定结果的顺序如何不同于源的顺序。



绝对保留订单。您可以通过索引将源元素映射到结果元素




  • AsEnumerable

  • Cast

  • Concat

  • 选择

  • ToArray

  • ToList



保存订单。元素被过滤,但不被重新排序。




  • 不同


  • 相交

  • OfType

  • 跳过

  • SkipWhile

  • 采取

  • TakeWhile

  • 其中

  • Zip(.net 4中的新增) li>


销毁订单 - 我们不知道预期的结果是什么顺序。




  • ToDictionary

  • ToLookup



显式重新定义订单 - 使用这些更改结果的顺序




  • OrderBy

  • OrderByDescending

  • 反向

  • ThenBy

  • ThenByDescending



根据一些规则重新定义订单。




  • GroupBy - 根据源中元素的顺序,IGrouping对象按顺序生成产生了每个IGrouping的第一个关键。分组中的元素按照它们在源中显示的顺序生成。

  • GroupJoin - GroupJoin保留外部元素的顺序,对于外部元素的顺序,保留内部匹配元素的顺序。

  • 加入 - 保留外部元素的顺序,对于每个这些元素,内部的匹配元素的顺序。

  • SelectMany - 对于源的每个元素,选择器被调用,并返回一个值序列。

  • 联盟 - 当此方法返回的对象被枚举,Union以该顺序枚举第一个和第二个,并产生尚未生成的每个元素。






编辑:我已经根据这个实施

  private static IEnumerable< TSource> DistinctIterator< TSource> 
(IEnumerable< TSource> source,IEqualityComparer< TSource> comparer)
{
Set< TSource> set = new Set< TSource>(comparer);
foreach(源代码中的TSource元素)
if(set.Add(element))yield return element;
}


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?

解决方案

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.

Preserves Order Absolutely. You can map a source element by index to a result element

  • AsEnumerable
  • Cast
  • Concat
  • Select
  • ToArray
  • ToList

Preserves Order. Elements are filtered, but not re-ordered.

  • Distinct
  • Except
  • Intersect
  • OfType
  • Skip
  • SkipWhile
  • Take
  • TakeWhile
  • Where
  • Zip (new in .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

  • OrderBy
  • OrderByDescending
  • Reverse
  • ThenBy
  • ThenByDescending

Redefines Order according to some rules.

  • GroupBy - The IGrouping objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping. Elements in a grouping are yielded in the order they appear in source.
  • GroupJoin - GroupJoin preserves the order of the elements of outer, and for each element of outer, the order of the matching elements from inner.
  • Join - preserves the order of the elements of outer, and for each of these elements, the order of the matching elements of inner.
  • SelectMany - for each element of source, selector is invoked and a sequence of values is returned.
  • Union - When the object returned by this method is enumerated, Union enumerates first and second in that order and yields each element that has not already been yielded.

Edit: I've moved Distinct to Preserving order based on this 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;
    }

这篇关于使用LINQ保留订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆