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
开发者_如何学PythonThanks
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() });
select count(1) as cnt, Name from mytable group by Name
Write a SQL query that creates this summary and execute it using ADO.NET.
If you want to use sql server. Below is the answer
Select Name, count(Name)
From YourTableNamew
Group by Name
SELECT COUNT(DISTINCT column_name) FROM table_name group by column_name
精彩评论