【问题标题】:Linq Enumerable<T>.ElementAt => IEnumerable<T>Linq Enumerable<T>.ElementAt => IEnumerable<T>
【发布时间】:2010-11-09 18:58:16
【问题描述】:

Linq 中是否有与ElementAt 相同的方法,只是它返回一个带有单个元素而不是实际元素的IEnumerable&lt;T&gt;?是不是有一些SelectRange(startIndex, endIndex) 方法我可以使用并且只传递相同的索引两次?

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    最简单的方法是使用

    source.Skip(count).Take(1)
    

    或更一般地

    source.Skip(startIndex).Take(endIndex - startIndex)
    

    (假设包含startIndex 但不包含endIndex)。

    【讨论】:

    • 如果sourceList,那么Skip(count)会是O(1)吗?
    • @Mark:不幸的是,我不相信 Skip 对此进行了优化,不。有点坑,真的:(
    【解决方案2】:

    啊.. 它叫GetRange(index, count)。我的错。刚刚找到了:)

    【讨论】:

    • 不,这是 List 而不是 IEnumerable&lt;T&gt; 的一部分,它的工作方式会大不相同(因为它很急切)。
    • @马克。仅适用于List&lt;T&gt;。乔恩的回答适用于任何IEnumerable
    • 但是 +1 在某些情况下仍然是一个值得注意的选择。
    • @Mark:它会立即复制项目,而 LINQ 序列是惰性的——它们只在您开始请求数据时查看数据。
    • @Mark - 那是我的玩笑,猜猜它没那么好笑。 :)
    【解决方案3】:

    Jon Skeet 的技术是一个很好的方法。然而,我建议可能 优化,它基于Enumerable.Skip 中的实现细节:它目前似乎没有利用IListIList&lt;T&gt; 上的索引器。幸运的是,Enumerable.ElementAt 可以。

    所以另一种解决方案是:

    var itemAsSequence = new[] { source.ElementAt(index) };
    

    请注意,这将急切地执行。如果您想要类似于 Jon 的答案的延迟执行语义,您可以执行以下操作:

    public static IEnumerable<T> ElementAtAsSequence<T>
      (this IEnumerable<T> source, int index)
    {
       // if you need argument validation, you need another level of indirection
    
       yield return source.ElementAt(index);
    }
    
    ...
    
    var itemAsSequence = source.ElementAtAsSequence(index);
    

    我应该指出,由于这依赖于实现细节,未来 LINQ to Objects 的改进可能会使这种优化变得多余。

    【讨论】:

      【解决方案4】:

      写一个扩展方法

          public static IEnumerable<T> ToMyEnumerable<T>(this T input)
          {
              var enumerbale = new[] { input };
              return enumerbale;
          }
      
          source.First( p => p.ID == value).ToMyEnumerable<T>()
      

      这是 O(n)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-17
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        相关资源
        最近更新 更多