---
title: "How to sum a field in a List"  
description: "How to sum a field in a List"  
author: "Anonymous User"  
published: 2014-09-05  
updated: 2023-07-06  
canonical: https://www.mindstick.com/forum/2247/how-to-sum-a-field-in-a-list  
category: "vb.net"  
tags: ["vb.net"]  
reading_time: 2 minutes  

---

# How to sum a field in a List

I have:

```
 Dim aList As List(Of myObj.Answers) = myObj.GetResults(someID)
```

How can I get the [sum](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq) of aList.AnswerCount [field](https://www.mindstick.com/forum/160867/does-mindstick-provide-training-for-field-marketing) in my List(Of myObj) ?

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that. Here are the steps on how to sum a field in a list in VB.NET:

1. Create a new Windows Forms project in VB.NET.
2. Add a ListBox control to the form.
3. In the code behind file, add the following code:

VB.Net

```plaintext
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Initialize the sum variable.
    Dim sum As Double = 0

    ' Iterate through the listbox items.
    For Each item As ListItem In ListBox1.Items
        ' Get the value of the item.
        Dim value As Double = item.Value

        ' Add the value to the sum.
        sum = sum + value
    Next

    ' Print the sum.
    Console.WriteLine(sum)
End Sub
```

1. Run the project. When you click the button, the code in the `Button1_Click` event handler will be executed. The code will iterate through the items in the ListBox control and add the values of the items to the sum variable. The sum variable will then be printed on the console.

Here is an explanation of the code:

- The `For Each` loop is used to iterate through the items in the ListBox control.
- The `Value` property of the ListItem control returns the value of the item.
- The `Sum` variable is used to store the sum of the values of the items in the ListBox control.
- The `Console.WriteLine` method is used to print the sum on the console.

### Reply by Sumit Kesarwani

Hi Chintoo, \
try this:\

aList.Sum(Function(i) i.AnswerCount)


---

Original Source: https://www.mindstick.com/forum/2247/how-to-sum-a-field-in-a-list

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
