如何使用C#中数据表中的键字组选择列和列的总和[英] How to select columns and sum of columns using group by keyword from data table in c#

本文是小编为大家收集整理的关于如何使用C#中数据表中的键字组选择列和列的总和的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个名为dttest的数据表.我只需要使用linq概念

从C#DataTable中选择列.

数据表名称为dttest.

示例:dttest包含以下数据,

在此处输入图像说明

我希望结果如下,即我需要基于group ino ...

的数量和数量列的总和

在此处输入图像说明 任何人都可以帮我解决这个问题....

推荐答案

DtTest
.AsEnumerable()
.GroupBy
(
   x=>
   new
   {
       BNO = x.Field<int>("BNO"),
       INO = x.Field<int>("INO"),
       Desp = x.Field<string>("Desp"),
       Rate= x.Field<decimal>("Rate")
   }
)
.Select
(
   x=>
   new 
   {
      x.Key.BNO,
      x.Key.INO,
      x.Key.Desp,
      Qty = x.Sum(z=>z.Field<int>("Qty")),
      x.Key.Rate,
      Amount = x.Sum(z=>z.Field<decimal>("Amount"))
   }
);

本文地址:https://www.itbaoku.cn/post/1557191.html

问题描述

i am having a DataTable called dtTest. i just need to select columns from the C# Datatable using Linq concepts

Data Table Name is DtTest.

Example : DtTest Contains the following data's,

enter image description here

i want the result as below, that is i need the SUM of Qty and Amount columns based on group INO...

enter image description here Can anyone Please help me to solve this....

推荐答案

DtTest
.AsEnumerable()
.GroupBy
(
   x=>
   new
   {
       BNO = x.Field<int>("BNO"),
       INO = x.Field<int>("INO"),
       Desp = x.Field<string>("Desp"),
       Rate= x.Field<decimal>("Rate")
   }
)
.Select
(
   x=>
   new 
   {
      x.Key.BNO,
      x.Key.INO,
      x.Key.Desp,
      Qty = x.Sum(z=>z.Field<int>("Qty")),
      x.Key.Rate,
      Amount = x.Sum(z=>z.Field<decimal>("Amount"))
   }
);