如何获得“件”列的总和在数据表中?

发布于 2024-12-12 04:02:55 字数 1108 浏览 0 评论 0原文

如何获得数据表中“件”列的总和?假设我有下表。如何计算article=“milk”和artno=“15”的“总”件数?

Columns:   article     artno    pieces
Rows:
1          milk        15       1
2          water       12       1
3          apple       13       2
4          milk        15       1
5          milk        16       1
6          bread       11       2
7          milk        16       4

我的新数据表的结果应该是这样的:

Columns:   article     artno    pieces
Rows:
1          bread       11       2
2          water       12       1
3          apple       13       2
4          milk        15       2
5          milk        16       5

我的代码:

foreach (DataRow foundDataRow in foundRows1)
{
    int i = 0;
    foreach (DataRow dataRow in foundRows2)
    {
        if (object.Equals(dataRow.ItemArray[0], foundDataRow.ItemArray[0])
            && object.Equals(dataRow.ItemArray[3], foundDataRow.ItemArray[3]))
        {
            i = i + Convert.ToInt16(dataRow.ItemArray[4]);
        }
    }
    Debug.Print(i.ToString());
}

抱歉,但我是新的数据库开发人员,我的英语口语不太好。

How can I get a sum for the column "pieces" in a datatable? Say I had the following table. How can I calculate the "total" pieces for article="milk" and artno="15"?

Columns:   article     artno    pieces
Rows:
1          milk        15       1
2          water       12       1
3          apple       13       2
4          milk        15       1
5          milk        16       1
6          bread       11       2
7          milk        16       4

The Result of the my new DataTable should be this:

Columns:   article     artno    pieces
Rows:
1          bread       11       2
2          water       12       1
3          apple       13       2
4          milk        15       2
5          milk        16       5

My Code:

foreach (DataRow foundDataRow in foundRows1)
{
    int i = 0;
    foreach (DataRow dataRow in foundRows2)
    {
        if (object.Equals(dataRow.ItemArray[0], foundDataRow.ItemArray[0])
            && object.Equals(dataRow.ItemArray[3], foundDataRow.ItemArray[3]))
        {
            i = i + Convert.ToInt16(dataRow.ItemArray[4]);
        }
    }
    Debug.Print(i.ToString());
}

Sorry, but i'm new DataBase developer and my english speak language is not so good.

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

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

发布评论

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

评论(4

溺渁∝ 2024-12-19 04:02:55

使用 DataTable.Compute< /a> 方法,你可以这样做:

int sum = (int)table.Compute("Sum(pieces)", "article = 'milk' AND artno='15'");

Using the DataTable.Compute method, you can do:

int sum = (int)table.Compute("Sum(pieces)", "article = 'milk' AND artno='15'");
为你鎻心 2024-12-19 04:02:55

您可以使用 AsEnumerable 方法,如下所示:

var results = dataTable.AsEnumerable()
                       .Where(row => row.Field<string>("article") == "milk" && 
                                     row.Field<int>("artno") == 15)
                       .Select(row => row.Field<int>("pieces"))
                       .Sum();

You could use the AsEnumerable method like so:

var results = dataTable.AsEnumerable()
                       .Where(row => row.Field<string>("article") == "milk" && 
                                     row.Field<int>("artno") == 15)
                       .Select(row => row.Field<int>("pieces"))
                       .Sum();
驱逐舰岛风号 2024-12-19 04:02:55

使用 Michael 的建议,减少 1 个步骤:(

var results = dataTable.AsEnumerable()
                       .Where(row => (row.Field<string>("article ") == "milk") &&
                                     (row.Field<int>("artno") == 15))
                       .Sum(row => row.Field<int>("pieces"));

这是使用 Linq to DataSet

Using Michael's suggestion with 1 less step:

var results = dataTable.AsEnumerable()
                       .Where(row => (row.Field<string>("article ") == "milk") &&
                                     (row.Field<int>("artno") == 15))
                       .Sum(row => row.Field<int>("pieces"));

(This is using Linq to DataSet)

ぺ禁宫浮华殁 2024-12-19 04:02:55

在 .NET 4 Windows 应用程序中,这段代码在 sum 变量中给出 9:

    DataTable dt = new DataTable("aaa");

    dt.Columns.Add("pieces", typeof(int));

    dt.Rows.Add(new object[] { 1 });
    dt.Rows.Add(new object[] { 3 });
    dt.Rows.Add(new object[] { 5 });

    var sum = dt.Compute("SUM(pieces)", string.Empty);

in a .NET 4 windows application, this snippet of code gives 9 in the sum variable:

    DataTable dt = new DataTable("aaa");

    dt.Columns.Add("pieces", typeof(int));

    dt.Rows.Add(new object[] { 1 });
    dt.Rows.Add(new object[] { 3 });
    dt.Rows.Add(new object[] { 5 });

    var sum = dt.Compute("SUM(pieces)", string.Empty);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文