---
title: "Getting count of all repeated rows in a datatable in C#"  
description: "Getting count of all repeated rows in a datatable in C#"  
author: "Allen Scott"  
published: 2015-01-18  
updated: 2015-01-18  
canonical: https://www.mindstick.com/forum/12872/getting-count-of-all-repeated-rows-in-a-datatable-in-c-sharp  
category: "asp.net"  
tags: ["c#", "database"]  
reading_time: 2 minutes  

---

# Getting count of all repeated rows in a datatable in C#

I am [filling](https://yourviews.mindstick.com/audio/1303/the-power-of-silence-what-happens-when-you-stop-filling-every-moment) a [dataset](https://www.mindstick.com/articles/19/dataset) by running a [stored procedure](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net), and i am filling a [datatable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) from the dataset.

DataSet RawDataSet = DataAccessHelper.RunProcedure("storedprocedureName");

//this will just return the dataset by running the procedure

DataTable RankTable = RawDataSet.Tables[0];

The Datatable has the below [values](https://www.mindstick.com/forum/327/sum-textbox-values):

Vicky [Login](https://www.mindstick.com/articles/12852/styles-login-form-in-android) 6/24/2014

Vicky Login 6/25/2014

Arun Game 6/26/2014

Arun Login 6/27/2014

Kumar Login 6/28/2014

Kumar Game 6/29/2014

Arun Login 6/30/2014

Arun Login 7/1/2014

Kumar Game 7/2/2014

Vicky Login 7/3/2014

Arun Login 7/4/2014

Arun Game 7/5/2014

I want to parse this dataTable and get how many times everyone has done an [action](https://www.mindstick.com/forum/155752/what-is-action-result-with-its-types), [i.e](https://answers.mindstick.com/qa/39877/what-state-was-the-first-to-enter-the-union-i-e-was-the-first-to-ratify-the-united-states-constitution) how many times each row is repeated:

Vicky Login 3

Arun Game 2

Arun Login 4

Kumar Login 1

Kumar Game 2

I am new to C#. This can be done using count(*) in SQL but I need to do this using C#. Please let me know if there are any ways to achieve this.

I have searched SO but all posts are related to removing the duplicates/finding the duplicates, which does not solve my purpose.

## Replies

### Reply by Anonymous User

You can achieve that using LINQ like:

```
var query = RankTable.AsEnumerable()                    .GroupBy(r => new { Name = r.Field<string>("Name"),
Action = r.Field<string>("Action") })                    .Select(grp=> new                    {                       
Name = grp.Key.Name,                       
Action = grp.Key.Action,                        Count = grp.Count()                    });
```

Here Name is assumed to be the column name for first column, and Action is assumed to be name of second column which has values like login, game etc.

For output use:

```
foreach (var item in query){    Console.WriteLine("Name: {0}, Action: {1}, Count: {2}",item.Name, item.Action,item.Count);}
```


---

Original Source: https://www.mindstick.com/forum/12872/getting-count-of-all-repeated-rows-in-a-datatable-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
