Mobster007 Mobster007 - 1 year ago 87
C# Question

How can you view the total sum from a list c#

GloryDays.Add(new Songs("Shout Out to My Ex", 200));
GloryDays.Add(new Songs("Touch", 161));
GloryDays.Add(new Songs("Power", 177));
GloryDays.Add(new Songs("Your Love", 210));
GloryDays.Add(new Songs("Nobody Like You", 180));
GloryDays.Sum(S => S.TrackLength);

This is what I got so far but I cannot seem to find a way to output the data. This project has to classes Albums and Songs and in songs class it contains accessors for TrackName and TrackLength.

Answer Source
 List<Songs> GloryDays = new List<Songs>();
 GloryDays.Add(new Songs { TrackName = "Shout Out to My Ex", TrackLength =200 });
 GloryDays.Add(new Songs { TrackName = "Touch", TrackLength = 161 });
 var result = GloryDays.Sum(t => t.TrackLength);
 textbox1.Text = result.ToString();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download