来自数据表行的C#min

时间:2013-06-06 10:10:11

标签: c# linq datatable

我有一个包含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

3 个答案:

答案 0 :(得分:3)

var result = dt.AsEnumerable()
               .Select(row => row.ItemArray.Cast<int>().Min());

答案 1 :(得分:2)

试试这个,

List<int> result = new List<int>();
foreach (DataRow row in table.Rows)
{
    result.Add(row.ItemArray.Cast<int>().Min());
}

答案 2 :(得分: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));

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

或shortend:

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());