Reputation: 3511
I have four columns name SrNo,RollNo,Name,Age
in my datatable and corresponding values as
SrNo ,Roll No,Name,Age
1, 1, ABC, 20
2, 2, DEF, 22
3, 3, ABC, 25
I want search how many different a names are present & their count.
Please suggest
Thanks
Upvotes: 1
Views: 254
Reputation: 1186
SELECT COUNT(DISTINCT column_name) FROM table_name group by column_name
Upvotes: 0
Reputation: 10105
If you want to use sql server. Below is the answer
Select Name, count(Name)
From YourTableNamew
Group by Name
Upvotes: 0
Reputation: 499072
Write a SQL query that creates this summary and execute it using ADO.NET.
Upvotes: 0
Reputation: 1500953
The simplest way to do this would probably be with LINQ (IMO, anyway):
var groups = table.AsEnumerable()
.GroupBy(x => x.Field<string>("Name"))
.Select(g => new { Name = g.Key, Count = g.Count() });
That's assuming you really do have the data in a DataTable
. If it's actually still in the database, you can use a similar LINQ to SQL query:
var groups = dataContext.GroupBy(x => x.Name)
.Select(g => new { Name = g.Key, Count = g.Count() });
Actually you could use an overload of GroupBy
to do it all in one method call:
var groups = dataContext.GroupBy(x => x.Name,
(key, group) => new { Name = key,
Count = group.Count() });
Upvotes: 1