1

I have a string CHF,2$DVC,1$PP,4 and i want result as this

List

     CHF
     DVC
     PP

Sum: 7

i can do it by spliting with '$', then apply foreach loop then again split with ',' and apply foreach loop.

On google i found that if i had string like string myString = "1,2,4,8,16"; the i can get the sum with .Sum(x => int.Parse(x)) but don't know how to implement in my case

Can anybody tell me how can i get desired result using linq without loop as i am new in linq

4
  • 1
    Can you explain how you come to a sum of 7? I am counting 8 characters but thats only a guess Commented Nov 27, 2013 at 12:30
  • @VahidNateghi you are right Commented Nov 27, 2013 at 12:32
  • you should write this down in your question. For me it was not clear you want the numbers, because your List contained 3 sets of letters. Commented Nov 27, 2013 at 12:38
  • yes but 3 sets of letters sum is 8 Commented Nov 27, 2013 at 12:42

1 Answer 1

2

you can try something like this

var t = "CHF,2$DVC,1$PP,4".Split('$').Select(s=>s.Split(','))
var list = t.Select(i=>i[0]).ToList();
var sum = t.Sum(i=>int.Parse(i[1]));
Sign up to request clarification or add additional context in comments.

3 Comments

I love intuitive variable names... :)
@gehho yep, but quickly :-)
@Grundy: Thanks that really quick and exactly i want :)

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.