Reputation: 886
I have a datatable with a column "No" and "Total" column. I'd like to bring back the sum of the "Total" column for each "No". I've tried the below, but I'm getting several errors as I'm struggling with the syntax.
var Values =
(from data in DtSet.Tables["tblDetails"].AsEnumerable()
group data by data.Field<"No">
select new
{
name_1 = data.Field<double>("No"),
name_2 = data.Field<double>("Total"),
}
);
Upvotes: 2
Views: 5256
Reputation: 109118
You start using linq, but good old DataTable
has it's own way:
var total = DtSet.Tables["tblDetails"].Compute("sum(Total)", "No = x");
I have to leave it with the x
in it, because I don't know the values of the "No" column. The part "No = x"
is a filter. If it is null or an empty string all rows will be used in the computation.
Upvotes: 1
Reputation: 236228
This will give you sum of Total
fields in name_2
property, and grouping No
in name_1
property (I think you need better naming here)
var Values = from row in DtSet.Tables["tblDetails"].AsEnumerable()
group row by row.Field<double>("No") into g
select new
{
name_1 = g.Key,
name_2 = g.Sum(r => r.Field<double>("Total"))
};
Consider about names No
and TotalSum
instead.
Upvotes: 5