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. 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++;
}
}
You can use
Linq-To-DatatableandEnumerable.GroupByto group by your function. Then you can useToDictionaryto build a dictionary with the function as key and the count as value:Note that it’s case sensitive by default. If you want it to be case insensitive you would need to pass the appropriate
IEqualityComparerfor the key, in this case: