1

I have a list i want to get the sum of this list & want to show it in a label

my list is

   List<double> Amount = new List<double>();

& it's filled up with integer values i need to get the sum of it.

i don't really know how to add values. tried some properties like compute, sum but it gives syntax error since it do not work with this.

here's how i am getting values in it

 double sales = double.Parse(split_values[1]);
                    listBox2.Items.Add(sales);
                    Amount.Add(sales);

& i need to show the output in a label

I also need to get top 3 values from this list but till yet i was unable to do so. .net Framework is 2.0

0

5 Answers 5

3

If you cannot use LINQ try something like this:

List<double> Amount = new List<double>();

var sum = 0.0;

Amount.ForEach(x => sum += x);
Sign up to request clarification or add additional context in comments.

1 Comment

it worked :D Thanks Dude. can you tell me a bit please about getting top 3 values from list plz @Alessandro D'Andria
2
using System;
using System.Collections.Generic;
using System.Linq;            

var amount = new List<double>() { 2.5, 1.5, 3.5, 5.5 };
            var sum = amount.Sum();
            Console.WriteLine(sum);

            var highest3 = amount.OrderByDescending(a => a).Take(3);

            var i = 1;
            foreach (var d in highest3)
            {
                Console.WriteLine("{0} is position {1}",d,i);
                i++;
            }

            Console.ReadLine();

Edit: Framework 2

var amount = new List<double>() { 2.5, 1.5, 3.5, 5.5 };
            var sum = 0.0d;
            foreach (var d in amount)
            {
                sum += d;
            }

            Console.WriteLine(sum);

            amount.Sort();
            amount.Reverse();

            lbl_first.Text = amount[0].ToString();
            lbl_second.Text = amount[1].ToString();
            lbl_third.Text = amount[2].ToString();

            var i = 1;
            foreach (var d in amount)
            {
                if (i > 3)
                    break;

                Console.WriteLine("{0} is position {1}",d,i);
                i++;
            }

            Console.ReadLine();

5 Comments

it's 2.0 so we can't use Sum etc. because it require Linq & Linq can be used in 4.0 :(
it's working but how can i print top 3 values in labels. plz :/ sorry i am Noooooooooob
Do you have the labels defined @CodingNoob? If so, you can do label1.text = d.ToString(); in the foreach loop
my lables are like lbl_first & lbl_second etc.
You've already accepted an answer...so, you don't need anymore help? I have edited the answer with your lables...you can ignore the console parts.
1

Sum of the list can be achieved in many ways, here is the simplest, Amount.Sum(); (Linq is required).

Concerning the label request, i haven't fully understood you.

3 Comments

i think we can't use linq in windows form. i am not able to add reference
for linq project must be vs4.0 or above.
oh i think i forgot to mention i am using 2.0
1
using System.Linq;

List<double> Amount = new List<double>() {1.0, 2.0, 3.0, 4.0};  
Console.WriteLine(Amount.Sum()); //10

var top3 = Amount.OrderByDescending(x => x).Take(3); // 4.0, 3.0, 2.0

3 Comments

i believe Sum needs some assembly reference that is not present in my windows form
@CodingNoob using System.Linq;
i forgot to mention i am using .Net Framework 2.0 & for linq it requires 4.0
1
double sales = Double.parseDouble(split_values[1]);//it's convert to double
Amount.add(sales);//value is added

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.