당신이 할 수있는 C#에서 annonymous 클래스의 그룹. 나는이 방법을 보여줍니다 다음 예제를 썼다 :
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
}
이 다음과 같은 출력 줄 것입니다 :
Voice CALLER_A Caller_B 2
Text CALLER_A Caller_C 2
VoiceMail CALLER_A Caller_B 1
@rsbarra : 우리는 모두 거의 같은 예제 코드를 작성을하지만 7 초 나를 이길 : -) –
@elian : 꽤 재미. =] 좋은 대답입니다! – rsbarro
고마워요. – kakopappa