3
| Dip |
12
i want to group it by category with LINQ and show the sum of the Counts(column) under Category
e.x.
Category | Counts
--------------------------------
1
|
202
2
|
295
3
|
36
DataTable MainData = obj.DATA();
var
query =
from
b
in
MainData.AsEnumerable()
group
b by b.Field<string>(
"
Category"
)
into
Grp
let
_count = Convert.ToInt32( Grp.FirstOrDefault().Field<string>(
"
_count"
))
orderby
__count
descending
select
new
Category= Grp.Key,
Count = _count
its just counting the rows.
This link
http://social.msdn.microsoft.com/Forums/en-US/3b13432a-861e-45f0-8c25-4d54622fbfb4/linq-group-and-sum-table?forum=linqprojectgeneral
[
^
] should give you the solution to your problem.
Part of your issue is that you aren't actually doing a sum in your linq.
var
result = MainData.AsEnumerable()
.Select(r=>
new
{ Category = r.Field<string>(
"
Category"
),
Count =Convert.ToInt32( r.Field<string>(
"
_count"
))})
.GroupBy(x=>x.Category).Select(g=>new{Category=g.Key, Count=g.Sum(c=>c.Count)});
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.