-2

Hi I'm working on Json Deserialize Object on C# using json newton. Below is my code.

 transactions = JsonConvert.DeserializeObject<List<TransactionClass>>(response.Content);

And returned results.

      [
        {
         "id":1,
         "school_uuid":"d17eab20-a442-11e9-928b-cb6a3dc8f7ea",
         "student_id":1,
         "actiontype":"1",
         "total":100,
         "created_at":"2019-07-12 01:54:45",
         "updated_at":"2019-07-12 01:54:45"
        },

        {
         "id":2,
         "school_uuid":"d17eab20-a442-11e9-928b-cb6a3dc8f7ea",
         "student_id":1,
         "actiontype":"1",
         "total":100,
         "created_at":"2019-07-12 01:55:28",
         "updated_at":"2019-07-12 01:55:28"
        }
     ]

I would like to sum for item total, In this case sum = 100 + 100 = 200. It work fine on looping method. But I prefer shorter like linq instead. Any advice or guidance on this would be greatly appreciated, Thanks.

joenpc npcsolution
  • 737
  • 4
  • 11
  • 25
  • [Are](https://stackoverflow.com/questions/14561312/) [you](https://stackoverflow.com/questions/29325589/) [too](https://stackoverflow.com/questions/1554926/) [lazy](https://stackoverflow.com/questions/641682/) [to](https://stackoverflow.com/questions/13230268/) [do](https://stackoverflow.com/questions/2978736/) [some](https://stackoverflow.com/questions/15417623/) [google](https://stackoverflow.com/questions/17717858/) [search](https://stackoverflow.com/questions/16250991/)? – vasily.sib Jul 12 '19 at 02:53
  • Dear vasily.sib , Thanks for answer. – joenpc npcsolution Jul 12 '19 at 02:57
  • @joenpcnpcsolution - Don't forget to use the `@` notification system so that your recipient gets notified. – Enigmativity Jul 12 '19 at 03:15

1 Answers1

2
using System.Linq;

int sumOfTotals = transactions.Sum(transaction => transaction.total);

Read this:

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sum?view=netframework-4.8

cdev
  • 5,043
  • 2
  • 33
  • 32