计算C#中数据表中所有重复行的数量

我通过运行存储过程填充数据集,并从数据集中填充数据表。

DataSet RawDataSet = DataAccessHelper.RunProcedure("storedprocedureName"); //this will just return the dataset by running the procedure DataTable RankTable = RawDataSet.Tables[0]; 

数据表具有以下值:

 Vicky Login 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 

我想解析这个dataTable并获得每个人做了多少次动作,即每行重复多少次:

 Vicky Login 3 Arun Game 2 Arun Login 4 Kumar Login 1 Kumar Game 2 

我是C#的新手。 这可以使用SQL中的count(*)来完成,但我需要使用C#来完成此操作。 如果有任何方法可以实现这一点,请告诉我。

我搜索过SO但所有post都与删除重复项/找到重复项有关,这并不能解决我的目的。

您可以使用LINQ实现这一点:

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

这里假设Name是第一列的列名, Action被假定为第二列的名称,其中包含登录,游戏等值。

你可能会看到LINQ to DataSet – MSDN

用于输出用途:

 foreach (var item in query) { Console.WriteLine("Name: {0}, Action: {1}, Count: {2}",item.Name, item.Action,item.Count); }