简体   繁体   English

如何计算C#中数据项在数据表的列中出现的次数

[英]How do I count the number of times an item appears in a column in a datatable in c#

I have created a datatable with two columns, "Function" and "Date". 我创建了一个包含两列的数据表:“函数”和“日期”。 This table records the date that each of n functions is used on a website. 该表记录了网站上n个功能中每个功能使用的日期。 I want to extract the number of times that a particular function such as "Access" appears in the table. 我想提取特定功能(例如“访问”)出现在表中的次数。 Any help will be appreciated. 任何帮助将不胜感激。

DataTable table = new DataTable();

table.Columns.Add("Function", typeof(string));
table.Columns.Add("Date", typeof(datetime));

table.Rows.Add(Access, 01/08/2012);
table.Rows.Add(Load, 01/08/2012);
table.Rows.Add(Save, 01/08/2012);
table.Rows.Add(Access, 02/08/2012);
table.Rows.Add(Print, 02/08/2012);

int accessCount = 0;

foreach (DataRow row in table.Rows)
{
    if (row["Function"] == "Access")
    {
        accessCount++;
    }
}
var results = table.AsEnumerable()
    .GroupBy(row => row.Field<string>("Function"))
    .Select(group => new 
    {
        Function = group.Key,
        Count = group.Count(),
    });

You can use linq for it 您可以使用linq

var result = from rec in table.AsEnumerable 
             group rec by rec.Field<string>("Function") into g 
             select new { Function = g.Key, Count = g.Count()}

You can use Linq-To-Datatable and Enumerable.GroupBy to group by your function. 您可以使用Linq-To-DatatableEnumerable.GroupBy对功能进行分组。 Then you can use ToDictionary to build a dictionary with the function as key and the count as value: 然后,您可以使用ToDictionary构建以功能为键,计数为值的字典:

var functionLookup = table.AsEnumerable()
    .GroupBy(r => r.Field<string>("Function"))
    .ToDictionary(g => g.Key, g => g.Count());

int accessCount = functionLookup["Access"];  // 2

Note that it's case sensitive by default. 请注意,默认情况下区分大小写。 If you want it to be case insensitive you would need to pass the appropriate IEqualityComparer for the key, in this case: 如果希望它不区分大小写,则需要为密钥传递适当的IEqualityComparer ,在这种情况下:

.ToDictionary(g => g.Key, g => g.Count(), StringComparer.OrdinalIgnoreCase);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算项目在列表C#中出现的次数 - Count number of times an item appears in a list C# 如何统计一个字符串在二维数组的某一列中出现的次数? - How to count the number of times a string appears in a column in a 2D array? 如何计算一个消息框在C#中出现多少次? - How do i go about counting how many times a messagebox appears in c#? 如何在另一个列表中创建每个项目的列表,以及该项目出现的次数? - How can I create a list of each item in another list, and the count of how many times that item appears? 我如何计算字典 c# 中相等值的数量 - how do I count the number of equal values in a dictionary c# 如何获取列表框中所选项目的编号(C#) - How do I get the number of the item selected in a listbox (C#) 计算单个特定值在列中出现的次数 - Count the number of times single specific value appears in column 如何使数据表中的每一行都引用Visual List C#中的项目? - How do I make each Row in a Datatable refer to an item in a Visual List C#? 计算“男性”和“女性”在访问数据库(asp.net,C#)的字段中出现多少次 - Count how many times 'male' and 'female' appears in a field in access database (asp.net, C#) 在C#中,如何识别不同的对,并计算一长对对中它发生了多少次? - In C#, how do I identify distinct pairs and count how many times it occurred in a long list of pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM