我有一个包含 6 列的数据表,我必须为每一行找到最小值。如何才能做到这一点?
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());
尝试这个,
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));
或者
dataTable.Select(row => new int[] { row.Col1, row.Col2, row.Col3, row.Col4, row.Col5, row.Col6 }).Select(intArray => intArray.Min());
或缩短:
dataTable.Select(row => (new int[] { row.Col1, row.Col2, row.Col3, row.Col4, row.Col5, row.Col6 }).Min()));
或者
dataTable.Select(row => row.ItemArray.Cast<int>().Min());
或者如果您的 dataTable 包含其他非整数列:
dataTable.Select(row => row.ItemArray.OfType<int>().Min());