C#:
public class Table
{
public string Text { get; set; }
public int Value { get; set; }
public int Count { get; set; }
}
static void Eg6()
{
string input = "PowerFail 50 Restart 30 PowerFail 50 Alarms 60 Alarms 60 Alarms 60 Chains 60 Lockets 60 Lockets 60 Alarms 60";
string[] array = input.Split(' ');
Table objTable;
List<table> lstTable = new List<table>();
int arrayCount = array.Length;
int count = 0;
while (count < arrayCount)
{
objTable = new Table();
objTable.Text = array[count];
count = count + 1;
objTable.Value = Convert.ToInt32(array[count]);
objTable.Count = 1;
lstTable.Add(objTable);
count = count + 1;
}
var finalTableResult = (from r in lstTable
group r by new { r.Text, r.Value } into result
select new
{
Text = result.Key.Text,
Value = result.Key.Value,
Count = result.Sum(x => x.Count)
}).ToList();
}
VB:
Public Class Table
Public Property Text() As String
Get
Return m_Text
End Get
Set
m_Text = Value
End Set
End Property
Private m_Text As String
Public Property Value() As Integer
Get
Return m_Value
End Get
Set
m_Value = Value
End Set
End Property
Private m_Value As Integer
Public Property Count() As Integer
Get
Return m_Count
End Get
Set
m_Count = Value
End Set
End Property
Private m_Count As Integer
End Class
Private Shared Sub Eg6()
Dim input As String = "PowerFail 50 Restart 30 PowerFail 50 Alarms 60 Alarms 60 Alarms 60 Chains 60 Lockets 60 Lockets 60 Alarms 60"
Dim array As String() = input.Split(" "C)
Dim objTable As Table
Dim lstTable As New List(Of Table)()
Dim arrayCount As Integer = array.Length
Dim count As Integer = 0
While count < arrayCount
objTable = New Table()
objTable.Text = array(count)
count = count + 1
objTable.Value = Convert.ToInt32(array(count))
objTable.Count = 1
lstTable.Add(objTable)
count = count + 1
End While
Dim finalTableResult = (From result In From r In lstTableGroup r By New From { _
r.Text, _
r.Value _
}New With { _
Key .Text = result.Key.Text, _
Key .Value = result.Key.Value, _
Key .Count = result.Sum(Function(x) x.Count) _
}).ToList()
End Sub
finalTableResult contains the result you wanted. Comment if you need any help.