Gopal Jite Gopal Jite - 9 days ago 6
C# Question

LINQ to DataSet group by with specific column

I have a dataset with following table

id domain email
1 google.com av@et.org
2 apple.com gopal@jt.com
3 abc.com av@et.org
4 global.com av@et.org
5 local.com gopaljite@gmail.com
6 xyz.com gopaljite@gmail.com
7 gpl.com gopal@jt.com
8 mno.com av@et.org
9 pqr.co.in gopaljite@gmail.com
10 aad.com av@et.org


Now I need output as following

Sr. Email Domain_Count
1 av@et.org 5
2 gopal@jt.com 2
3 gopaljite@gmail.com 3


How can I do this with LINQ ?

Thanks in advance.

Answer
var result=table.GroupBy(c => c.email)
                            .Select(group => new { 
                                  Email = group.Key, 
                                 Domain_Count = group.Count() 
                            }).OrderByDescending(x=> x.Domain_Count);

bind to GridView:

yourgidView.DataSource = result.ToList();