【问题标题】:How to go to particular Item in IEnumerable如何转到 IEnumerable 中的特定项目
【发布时间】:2010-05-26 10:30:56
【问题描述】:

我有 IEnumerable,其中包含数字数据。

编辑 IEnumerable 来自 System.Collection.Ienumerable 指令。

附上Viual Studio的snapShot,包含数据的Enum:

alt text http://www.freeimagehosting.net/uploads/bd72c6c310.jpg

简单介绍一下上面的图片,eLevelData 是IEnumerable 变量,其中有我的数据。

现在我想转到索引 4 或 5 处的数据,但我不想使用 foreach 循环。请有任何建议。

谢谢,

【问题讨论】:

    标签: c# .net silverlight silverlight-3.0 ienumerable


    【解决方案1】:

    var item = eLevelData.ElementAt(index);

    如果您的集合类型为 IEnumerable 而不是 IEnumerable<T>,您需要使用 Cast 扩展方法才能调用 ElementAt,例如

    var item = eLevelData.Cast<RMSRequestProcessor.RMSMedia>().ElementAt(index)

    【讨论】:

    • 嗨,李,我没有得到 ElementAt() 方法。我在 Silverlight 中使用它
    • @Subhen - 页面顶部有using System.Linq 指令吗?它适用于我的 Silverlight 3。
    【解决方案2】:

    不太了解 Silverlight 中可用的 .NET BCL/LINQ 子集,但 Skip 应该可以解决问题。但一般来说它内部还是使用foreach

    var item = eLevelData.Skip(4 /* or 5 */).First();
    

    【讨论】:

    • ElementAt 也是如此,但它比Skip(x).First() 更直接地得到答案
    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2015-01-29
    • 2019-02-27
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多