且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何选择数据表中列的最小值和最大值?

更新时间:2022-12-10 15:02:48

int minAccountLevel = int.MaxValue;
int maxAccountLevel = int.MinValue;
foreach (DataRow dr in table.Rows)
{
    int accountLevel = dr.Field<int>("AccountLevel");
    minAccountLevel = Math.Min(minAccountLevel, accountLevel);
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
}

是的,这确实是最快的方式.使用 Linq MinMax 扩展总是比较慢,因为你必须迭代两次.您可能会使用 Linq Aggregate,但语法不会比现在更漂亮.

Yes, this really is the fastest way. Using the Linq Min and Max extensions will always be slower because you have to iterate twice. You could potentially use Linq Aggregate, but the syntax isn't going to be much prettier than this already is.