1

Suppose you have a list<string[]>

  List<string[]> lst = new List<string[]>();

  lst.Add(new string[] { "A", "100.10" });
  lst.Add(new string[] { "B", "250.49" });

How do you get a sum of the second array items in the list?

Will need to convert the second array items to double and sum them up. Expected result is 350.59.

Thank you!

0

3 Answers 3

10

You could use:

double sum = lst.Sum(i => double.Parse(i[1]));
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it list this:

lst
.Select(item => Decimal.Parse(item[1]))
.Sum()

Comments

1
double total = 0;

foreach(string[] stringArray in lst)
{
    total += Convert.ToDouble(stringArray[1]);
}

2 Comments

Surely you mean ToDecimal()?
@DaveShaw I'd actually think he'd way ToDouble(), considering he's adding into a double...

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.