我有一个包含 4 列的数据表,如下所示:
private static DataSet dataSet;
private const string tableName = "MyTable";
private const string columnName1 = "Supplier"; //Column names
private const string columnName2 = "Invoice";
private const string columnName3 = "Item";
private const string columnName4 = "Amount";
我按供应商、发票列对表进行了分组,并使用以下 linq 查询计算了金额的总和:
private static DataTable GroupQueryA(DataTable dataTable)
{
DataTable groupedTable = dataTable.AsEnumerable()
.GroupBy(r => new { Key1 = r.Field<string>(columnName1), Key2 = r.Field<string>(columnName2) })
.Select(g => new GroupSum
{
Key1 = g.Key.Key1,
Key2 = g.Key.Key2,
Sum = g.Sum(x => x.Field<double>(columnName4))
}).PropertiesToDataTable<GroupSum>();
return groupedTable;
}
我声明的 GroupSum 类型为:
private class GroupSum
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public Double Sum { get; set; }
}
守着星空守着你
相关分类