使用LINQ获得二维数组的最小值和最大值 [英] Obtaining the min and max of a two-dimensional array using LINQ

查看:53
本文介绍了使用LINQ获得二维数组的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用LINQ获得二维数组的最小值和最大值?需要明确的是,我的意思是数组中所有项目的最小值/最大值(而不是特定维度的最小值/最大值).

How would you obtain the min and max of a two-dimensional array using LINQ? And to be clear, I mean the min/max of the all items in the array (not the min/max of a particular dimension).

还是我只需要循环使用老式方式?

Or am I just going to have to loop through the old fashioned way?

推荐答案

由于Array实现了IEnumerable,因此您可以执行以下操作:

Since Array implements IEnumerable you can just do this:

var arr = new int[2, 2] {{1,2}, {3, 4}};
int max = arr.Cast<int>().Max();    //or Min

这篇关于使用LINQ获得二维数组的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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