C# DataTable中返回列中的最大值

https://blog.csdn.net/weixin_34203832/article/details/90625019

此处以表dt2中的keyIndex列(int类型)为例
1、通过linq来实现
int maxKeyIndex = dt2.AsEnumerable().Select(t => t.Field<int>("keyIndex")).Max();
Linq语法: 点击打开链接
2、 通过 Compute方法来实现
int ee = (int)dt2.Compute("Max(keyIndex)", "true");
Compute方法:点击打开链接
3、 通过 Select方法来实现
int rr = (int)dt2.Select("", "keyIndex DESC")[0]["keyIndex"];
Select方法:点击打开链接
4、转List
将DataTable中需要排序的列转List,然后通过list的Sort()方法来排序,默认值升序的即排序完成后,list中的最后一个是最大值。

方法一:使用ArrayList
using System.Collections;
int[] arr = {2,4,5,6,6,55,66,89,105,669,569 };
ArrayList list = new ArrayList(arr);
list.Sort();
int min = Convert.ToInt32(list[0]);
int max = Convert.ToInt32(list[list.Count - 1]);
Console.WriteLine("最大" + max +" 最小:"+min);
方法二:用?:语句比较简单
for(int i=0;i<array.length;i++)
{
int a=0;b=0;
a>a[i]?(b=a):(b=a[i]);只要一轮for循环就能得出最大值,同理求出最小值.
}

C# 获取DataTable某列的最大值
string a = this.dt.AsEnumerable().Max(s => Convert.ToInt32(s.Field("UNCHIN_CD")));

原文地址:https://www.cnblogs.com/sunny3158/p/15303633.html