Reputation: 5095
I have a list like this,
TYPE FROM TO
voice CALLER_A CALLER_B
text CALLER_A CALLER_C
voicemail CALLER_A CALLER_B
voice CALLER_A CALLER_B
text CALLER_A CALLER_C
I want to have a count How many time FROM Called TO By using TYPE
TYPE FROM TO COUNT
voice CALLER_A CALLER_B 2
voicemail CALLER_A CALLER_B 1
text CALLER_A CALLER_C 2
How to I do this. Please Advice
Upvotes: 1
Views: 68
Reputation: 19067
You can group on a annonymous class in C#. I wrote the following example that demonstrates this:
void Main()
{
// This is the list from your example.
var contactmoments = new List<ContactMoment> {
new ContactMoment { From = "CALLER_A", To = "Caller_B", Type = ContactType.Voice },
new ContactMoment { From = "CALLER_A", To = "Caller_C", Type = ContactType.Text },
new ContactMoment { From = "CALLER_A", To = "Caller_B", Type = ContactType.VoiceMail },
new ContactMoment { From = "CALLER_A", To = "Caller_B", Type = ContactType.Voice },
new ContactMoment { From = "CALLER_A", To = "Caller_C", Type = ContactType.Text }
};
// Group by the properties 'From', 'To' and 'Type'
var groups = contactmoments.GroupBy(c => new { c.From, c.To, c.Type });
// Write the properties of the key and the size of the group to the console.
foreach(var group in groups)
{
Console.WriteLine("{0,-15} {1,-15} {2,-15} {3}", group.Key.Type, group.Key.From, group.Key.To, group.Count());
}
}
class ContactMoment
{
public string From { get; set; }
public string To { get; set; }
public ContactType Type { get; set; }
}
enum ContactType
{
Voice = 1,
Text = 2,
VoiceMail = 3
}
This will give the following output:
Voice CALLER_A Caller_B 2
Text CALLER_A Caller_C 2
VoiceMail CALLER_A Caller_B 1
Upvotes: 1
Reputation: 27369
If I understand the question correctly, something like this should work:
void Main()
{
var list = new List<CallRecord>();
list.Add(new CallRecord { Type="voice", From="CALLER_A", To="CALLER_B" });
list.Add(new CallRecord { Type="text", From="CALLER_A", To="CALLER_C" });
list.Add(new CallRecord { Type="voicemail", From="CALLER_A", To="CALLER_B" });
list.Add(new CallRecord { Type="voice", From="CALLER_A", To="CALLER_B" });
list.Add(new CallRecord { Type="text", From="CALLER_A", To="CALLER_C" });
var groups = (from cr in list
group cr by new {cr.Type, cr.From, cr.To}
into g
select g);
foreach(var group in groups)
Console.WriteLine("{0} - Count: {1}", group.Key, group.Count());
}
public class CallRecord
{
public string Type { get; set; }
public string From { get; set; }
public string To { get; set; }
}
Upvotes: 1