usr021986
usr021986

Reputation: 3511

Count & Search the Data on DataTable

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

Answers (5)

run
run

Reputation: 1186

SELECT COUNT(DISTINCT column_name) FROM table_name group by column_name

Upvotes: 0

Pankaj
Pankaj

Reputation: 10105

If you want to use sql server. Below is the answer

Select Name, count(Name)
From YourTableNamew 
Group by Name

Upvotes: 0

ibram
ibram

Reputation: 4579

select count(1) as cnt, Name from mytable group by Name

Upvotes: 1

Oded
Oded

Reputation: 499072

Write a SQL query that creates this summary and execute it using ADO.NET.

Upvotes: 0

Jon Skeet
Jon Skeet

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

Related Questions