开发者

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

开发者_如何学Python

Thanks


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜