从包含多个表的数据集中找到最小值和最大值

发布于 2024-12-08 15:10:23 字数 697 浏览 0 评论 0原文

我正在开发一个 asp.net 应用程序,其中我有一个包含 3 个表的数据集,即 ds.Tables[0]、Tables[1] 和 Tables[2]

我需要找到最小数量和最大数量数据集的所有 3 个表。怎么做呢?

我在 SO 中发现了类似的问题在这里但不确定这将如何帮助我实现我的目标?

更新:

 double minValue = double.MinValue;
 double maxValue = double.MaxValue;
 foreach (DataRow dr in dtSMPS.Rows)
 {
            double actualValue = dr.Field<double>("TOTAL_SUM");  // This fails specifying type cast error
            minValue = Math.Min(minValue, actualValue);
            maxValue = Math.Max(maxValue, actualValue);
 }

请指导!谢谢。

I am developing an asp.net application where i have a dataset having 3 tables i.e. ds.Tables[0], Tables[1] and Tables[2]

I need to find minimum number and maximum number out of all the 3 tables of dataset. How to do that?

I found similar question in SO here but not sure how that would help me getting my way?

UPDATED:

 double minValue = double.MinValue;
 double maxValue = double.MaxValue;
 foreach (DataRow dr in dtSMPS.Rows)
 {
            double actualValue = dr.Field<double>("TOTAL_SUM");  // This fails specifying type cast error
            minValue = Math.Min(minValue, actualValue);
            maxValue = Math.Max(maxValue, actualValue);
 }

Please guide! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怕倦 2024-12-15 15:10:23

您可以使用添加到已链接到的解决方案中的额外 for 循环。
IE

int minAccountLevel = int.MaxValue;
int maxAccountLevel = int.MinValue;

for (int i = 0; i < 3; i++)
{
var table = ds.Tables[i];
foreach (DataRow dr in table.Rows)
{
    int accountLevel = dr.Field<int>("AccountLevel");
    minAccountLevel = Math.Min(minAccountLevel, accountLevel);
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
}
}

You could use an extra for loop added to the solution you have already linked to.
I.e.

int minAccountLevel = int.MaxValue;
int maxAccountLevel = int.MinValue;

for (int i = 0; i < 3; i++)
{
var table = ds.Tables[i];
foreach (DataRow dr in table.Rows)
{
    int accountLevel = dr.Field<int>("AccountLevel");
    minAccountLevel = Math.Min(minAccountLevel, accountLevel);
    maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
}
}
柠檬色的秋千 2024-12-15 15:10:23

如果它们具有相同的列名,假设为“decimalValue”,则:

decimal minAccountLevel = decimal.zero;
decimal maxAccountLevel = decimal.zero;
for (int i = 0; i < dts.Tables.Count; i++)
        {
            foreach (DataRow dr in dts.Tables[i].Rows)
            {
                decimal accountLevel = decimal.Parse(dr["decimalValue"].ToString());
                minAccountLevel = Math.Min(minAccountLevel, accountLevel);
                maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
            }
        }

If they have the same column name suppose 'decimalValue' then:

decimal minAccountLevel = decimal.zero;
decimal maxAccountLevel = decimal.zero;
for (int i = 0; i < dts.Tables.Count; i++)
        {
            foreach (DataRow dr in dts.Tables[i].Rows)
            {
                decimal accountLevel = decimal.Parse(dr["decimalValue"].ToString());
                minAccountLevel = Math.Min(minAccountLevel, accountLevel);
                maxAccountLevel = Math.Max(maxAccountLevel, accountLevel);
            }
        }
温暖的光 2024-12-15 15:10:23

您可以使用 < 为每个数据表选择最小值和最大值每个 DataTable 上的 code>Compute 方法。

免责声明这不是安全代码;您应该检查错误、空值,并使其更具可读性。

像这样的东西应该有效:

decimal max = Math.Max(Convert.ToDecimal(ds.Tables[2].Compute("MAX(AccountLevel)","").ToString()),  Math.Max(Convert.ToDecimal(ds.Tables[0].Compute("Max(AccountLevel)","").ToString()),Convert.ToDecimal(ds.Tables[1].Compute("Max(AccountLevel)","").ToString()));

you can select the min and max for each DataTable using the Compute method on each DataTable.

Disclaimer this is not safe code; you should check for errors, nulls, and make it more readable.

Something like this should work:

decimal max = Math.Max(Convert.ToDecimal(ds.Tables[2].Compute("MAX(AccountLevel)","").ToString()),  Math.Max(Convert.ToDecimal(ds.Tables[0].Compute("Max(AccountLevel)","").ToString()),Convert.ToDecimal(ds.Tables[1].Compute("Max(AccountLevel)","").ToString()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文