0

I have a datatable with 6 columns, and I have to find the minimum value, for each row. How can this be done?

Col1 Col2 Col3 Col4 Col5 Col6 Min
45   41   24   25   74   145  24
27   28   398  82   2    54   2
5   563    7   20   43   254  5
Rocshy
  • 3,391
  • 11
  • 39
  • 56

3 Answers3

3
var result = dt.AsEnumerable()
               .Select(row => row.ItemArray.Cast<int>().Min());
cuongle
  • 74,024
  • 28
  • 151
  • 206
2

try this,

List<int> result = new List<int>();
foreach (DataRow row in table.Rows)
{
    result.Add(row.ItemArray.Cast<int>().Min());
}
petchirajan
  • 4,152
  • 1
  • 18
  • 20
0
dataTable.Select(row => Math.Min(Math.Min(Math.Min(Math.Min(Math.Min(row.Col1, row.Col2), row.Col3), row.Col4), row.Col5), row.Col6));

or

dataTable.Select(row => new int[] { row.Col1, row.Col2, row.Col3, row.Col4, row.Col5, row.Col6 }).Select(intArray => intArray.Min());

or shortend:

dataTable.Select(row => (new int[] { row.Col1, row.Col2, row.Col3, row.Col4, row.Col5, row.Col6 }).Min()));

or

dataTable.Select(row => row.ItemArray.Cast<int>().Min());

or if your dataTable contains other columns that aren't ints:

dataTable.Select(row => row.ItemArray.OfType<int>().Min());
SynerCoder
  • 12,493
  • 4
  • 47
  • 78