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
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
var result = dt.AsEnumerable()
.Select(row => row.ItemArray.Cast<int>().Min());
try this,
List<int> result = new List<int>();
foreach (DataRow row in table.Rows)
{
result.Add(row.ItemArray.Cast<int>().Min());
}
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());