I have an array of structures:
Public Structure emp
Public empName As String
Public empAge As Int32
Public empsal As Int32 ' salary
End Structure
An array of the above structure with 30 element. I want to sum the empsal of those emp elements whose emp.empName & emp.empAge is equal.
for ex.
emp(0).empName = "test"
emp(0).empAge = 32
emp(0).empsal = 10000
emp(1).empName = "test"
emp(1).empAge = 32
emp(1).empsal = 10000
emp(2).empName = "test12"
emp(2).empAge = 32
emp(2).empsal = 10000
How can this be done so that the result will be (test,20000) and (test12,10000)?
Using .NET 3.5
EDIT: Edited the samples to include the age in the key, and to end up with an
emparray.Absolutely. You should look at LINQ for this – you basically want to group by name and age, and then sum the salary. In C# you’d write something like:
I think in VB this would be something like:
See the
GroupBydocumentation for more details.As an aside, I would recommend:
int)empprefix from the fields/properties – they’re already part of an employee type, after all