我是一个LINQ新手尝试使用它列表中选择一个提供的索引值范围内,以acheive如下:如何使用LINQ
我有INTS的列表: -
List<int> intList = new List<int>(new int[]{1,2,3,3,2,1});
现在,我想使用LINQ比较前三个元素[索引范围0-2]与最后三个[索引范围3-5]之和。我尝试了LINQ选择和Take扩展方法还有的SelectMany方法,但我无法弄清楚如何这样说
(from p in intList
where p in Take contiguous elements of intList from index x to x+n
select p).sum()
我看着包含扩展方法太,但并不见得到我想要什么。有什么建议么?谢谢。
注意list .GetRange效率会更高,虽然对于较大的集合:https://开头icodeit。 wordpress.com/2012/08/27/performance-of-skip-and-take-in-linq-to-objects/,http://geekswithblogs.net/BlackRabbitCoder/archive/2012/03/29/c.net -little-wonders-skip-and-take.aspx –
nawfal
2015-07-25 09:40:14
这是一个很好的,有效的评论(几年前我这样认为有效,下面的答案作者说了同样的事),尽管OP特别询问了关于LINQ的问题你不能回忆y保证列出您的来源。这可能是有问题的简化版本,简化版本只是一个硬编码列表(并且可能确实是对象图或LINQ to SQL查询的查询)。 –
2015-07-27 18:48:03