Home >Backend Development >C#.Net Tutorial >Return the maximum value in a column in C# DataTable

Return the maximum value in a column in C# DataTable

黄舟
黄舟Original
2017-02-16 10:44:062002browse

Here we take the keyIndex column (int type) in table dt2 as an example

1. Implement it through linq

int maxKeyIndex = dt2.AsEnumerable().Select(t => t.Field<int>("keyIndex")).Max();

Linq syntax: click to open the link
2, implemented through the Compute method

int ee = (int)dt2.Compute("Max(keyIndex)", "true");

Compute method: click to open the link
3, implemented through the Select method

##

int rr = (int)dt2.Select("", "keyIndex DESC")[0]["keyIndex"];

Select method: Click to open the link4. Convert to List

Convert the columns that need to be sorted in the DataTable to List, and then use the Sort() of the list Method to sort, the default value is in ascending order, that is, after the sorting is completed, the last one in the list is the maximum value.

ListIntroduction

The above is the content of returning the maximum value in the column in C# DataTable, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!


##

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# memory managementNext article:C# memory management