There is an array of DataRow[] datarows . One of the columns is called Sum . How to get the maximum value for this column using the LINQ extension? Thank.

  • In datarow list of users and passwords, respectively. When adding a new user, I need to know the last ID. - Alexander Puzanov
  • stored in one of the SQLite tables - Alexander Puzanov
  • one
    You can also use Dapper for this task (in fact, these are extensions for ADO.NET), because working with pure ADO.NET is not as effective. - Vadim Ovchinnikov

1 answer 1

If we leave aside the fact that you are working with an array of DataRow , and answer specifically the question, then the search for the maximum element will not be very different from the search for an element in the array containing objects of a particular class.

You can access DataRow columns using indexers (by column name or index (count from 0)), which return an object of type.

You can also add a link to System.Data.DataSetExtensions.dll and use Field extension methods that provide strongly typed access to your columns.

Thus, your code will be approximately as follows:

  var max = datarows .Select(x => x.Field<int>("Sum")) .Max();