How to sum similar element of an array of structure in vb.net?

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)</b>?

Using .NET 3.5


EDIT: Edited the samples to include the age in the key, and to end up with an emp array.

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:

var query = employees.GroupBy (x => new { x.empName, x.empAge }, x => x.empSal, (key, salaries) => new emp { empName = key.empName, empAge = key.empAge, empSalary = salaries.Sum() }) var array = query.ToArray();

I think in VB this would be something like:

Dim query = employees.GroupBy( _ Function(x) New With { Key .Name = x.empName, Key .Age = x.empAge }, _ Function(x) x.empSal, _ Function(k, salaries) New Emp With _ { .empName = k.Name, .empAge = k.Age, .empSalary = salaries.Sum() }_ ) Dim array = query.ToArray()

See the GroupBy documentation for more details.

As an aside, I would recommend:

    <li>Giving your type a fuller name which follows .NET naming conventions</li> <li>Making it a class rather than a structure (it's not really a single value, like an int)</li> <li>Using properties instead of public fields</li> <li>Removing the emp prefix from the fields/properties - they're already part of an employee type, after all</li> </ul>


    Grouping by name and age you could do something like the following in C#:

    var query= from e in employees group e by new {e.empName, e.empAge} into g select new {Name=g.Key, Value=g.Sum(x=> x.empSal)};

    In VB (per a converter):

    Dim query= From g In From e In employeesGroup e By New From { _ e.empName, _ e.empAge _ }New With { _ Key .Name = g.Key, _ Key .Value = g.Sum(Function(x) x.empSal) _ }


  • Creating a new table and setting the expiration date in bigquery using Python
  • Hide/show the selected option on a secondary select
  • PHP Replace multidimensional array keys
  • Text manipulation in Spark and Scala
  • Beginner sql join query
  • Single-row subquery returns more than one row
  • How do I give each person in this list a raise
  • How to store pointer to S3 objects in Amazon SimpleDB?
  • Java catching exceptions and subclases
  • Do stored procedures have the ability to delete a file from the OS?
  • Imagemagick set interline spacing?
  • Insert Pandas dataframe into Cassandra Table
  • Finding number of samples in a .wav header
  • How to not need user input for install.packages(type = “both”)
  • send data back from jsp iterator to struts action class
  • Getting data from database using HQL
  • FTPWebRequest .NET 3.5 vs 4
  • Allocating a 2D contiguous array within a function
  • Is mp4 stream able with ffserver?
  • how to display   in Mozilla using XSL.
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Fetching methods from BroadcastReceiver to update UI
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • JSON with duplicate key names losing information when parsed
  • Symfony2: How to get request parameter
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • GridView Sorting works once only
  • php design question - will a Helper help here?
  • Buffer size for converting unsigned long to string
  • AngularJs get employee from factory
  • WPF Applying a trigger on binding failure
  • Hits per day in Google Big Query
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4