LINQ查询如何在开始索引和结束索引之间选择最大值 [英] LINQ Query How to select Max value between start and end index

查看:112
本文介绍了LINQ查询如何在开始索引和结束索引之间选择最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用LINQ查询数字数组以获取起始索引和终止索引之间的最大值.

How do I use LINQ to query an array of numbers to get the highest value between start index and end index.

int []数字= {1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1};

int[] numbers = { 1, 2, 3, 4 ,5 ,6 ,7 ,8 ,9 ,8 ,7 ,6, 5 ,4 ,3 ,2 ,1};

我想找到数字[5]和数字[8]之间的最大值

I want to find the Max value between numbers[5] and numbers[8]

谢谢

迈克

推荐答案

您需要跳过四个元素,然后取下四个元素的最大值(#5 ..#8,包括首尾):

You need to skip four elements, then take the max of the next four (#5..#8, inclusive):

var begin = 5;
var end = 8;
var res = numbers.Skip(begin-1).Take(end-begin+1).Max();

这篇关于LINQ查询如何在开始索引和结束索引之间选择最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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