计数DataTable中列值的出现次数 - 整数

时间:2016-05-31 20:18:32

标签: c# linq datatable counter

我需要计算几个不同列的出现次数。例如,我的DataTable列如下所示,它们只是字符串和日期类型

[Case___], [Status],[Summary] ,[Description], 
[assigned_group], [Assigned_Individual], [Alert_Status],
[Date_Time_Created], [Date_Time_Resolved] ,
[Date_Time_Closed] ,[Data_Output_Type]

对于每个**** Assigned_Individual **我需要计算

此人已解决状态||的次数已关闭

此人有 Alert_Status ='SLA提醒'且状态<>的次数'已解决'和状态<> '闭合'

此人有 Alert_Status ='SLA未接及'和Data_Output_Type ='有效门票'

的次数

我的目标是返回一个DataTable或List,其中包含以下记录

Assigned_Indivual - 已解决 - SLA警报 - SLA错过

鲍勃,5,1,4 玛丽,2,1,3 阿什利,7,0,3

我只是在寻找方向,因为我知道我可能需要使用LINQ我甚至无法理解这样做的方法。

提前感谢Stack Oracles!

我能够获得一个人拥有身份的次数==“已解决”||状态==使用下面的代码“关闭”

var groupedData = from b in dt.AsEnumerable()
                          where b.Field<string>("Status") == "Resolved" || b.Field<string>("Status") == "Closed"
                          group b by new
                          {
                              Assigned_Individual = b.Field<string>("Assigned_Individual"),
                              Status = b.Field<string>("Status"),
                              assigned_group = b.Field<string>("assigned_group")
                          }
                              into g

                              let Counter = g.Count()
                              select new
                              {
                                  Assigned_Individual = g.Key.Assigned_Individual,
                                  Status = g.Key.Status,
                                  assigned_group = g.Key.assigned_group,
                                  Count = Counter
                              };

返回一个计数,例如bob,5 OR Mary,2 但我需要知道如何在不使用where条件的情况下执行此操作,以便我可以计算上面列出的3个条件。

1 个答案:

答案 0 :(得分:1)

// Make a datatable Result
var dtResult = new DataTable();
dtResult.Columns.Add("Assigned_Indivual");
dtResult.Columns.Add("Resolved");
dtResult.Columns.Add("SLA Alert");
dtResult.Columns.Add("SLA Missed");

// Get All assigneds
var assigneds = dTable.AsEnumerable().Select(a => a.Field<string>("Assigned_Individual")).Distinct();

foreach(var assigned in assigneds)
{
    // Get All results os assigned indexed
    var resultOfAssigned = dTable.AsEnumerable().Where(a => a.Field<string>("Assigned_Individual") == assigned).ToList();

    // Count of results
    var resultResolved = resultOfAssigned.Where(a => a.Field<string>("Status") == "Closed").Count();
    var resultAlert = resultOfAssigned.Where(a => a.Field<string>("Alert_Status") == "SLA Alert" && a.Field<string>("Status") == "Resolved" && a.Field<string>("Status") == "Closed").Count();
    var resultMissed = resultOfAssigned.Where(a => a.Field<string>("Alert_Status") == "SLA Missed" && a.Field<string>("Data_Output_Type") == "Active Tickets").Count();

    // Define a data row
    var dRow = dtResult.NewRow();
    dRow[0] = assigned;
    dRow[1] = resultResolved;
    dRow[2] = resultAlert;
    dRow[3] = resultMissed;

    // Insert datarow in Datatable result
    dtResult.Rows.Add(dRow);
}