List中的 LINQ 项目范围
使用 Linq,如何获取序数位置在 5 到 27 之间的对象列表。
MyObject 的数量未知,因此什么也找不到。
"allItems" 始终按顺序排列,因此需要序号位置。
像这样:
List<MyObject> allItems = GetAllItems();
List<MyObject> myRange = allItems.LINQSTATEMENTHERE(5 to 27th items);
Using Linq, how can I get a list of objects where their ordinal poistion is between 5 and 27.
There is an unknown number of MyObject, so somethings nothing could be found.
"allItems" is always in order so ordinal position is required.
Like this:
List<MyObject> allItems = GetAllItems();
List<MyObject> myRange = allItems.LINQSTATEMENTHERE(5 to 27th items);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Skip() 和 Take() 的组合:
Use a combination of Skip() and Take():
由于您需要立即执行(您希望将结果加载到列表中),因此这里不需要使用 LINQ;您可以使用实例
GetRange
方法List
:当然,如果您想要延迟执行,或者希望该技术应用于其他类型的序列,请使用 Justin Niessner 的技术。他的技术的一个好处是,如果代表范围的元素太少,它就不会爆炸。
Since you require immediate execution (you want to load the results into a list), there's no need to use LINQ here; you can use the instance
GetRange
method onList<T>
:Of course, if you want deferred execution, or you want the technique to apply to other types of sequences, go with Justin Niessner's technique. The one benefit that his technique has is that it won't blow up if there are too few elements to represent the range.