Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1015939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:28:31+00:00 2026-05-16T10:28:31+00:00

I’m using VB.NET on Framework 2.0. I’m looking to quickly group a Generic List<>

  • 0

I’m using VB.NET on Framework 2.0. I’m looking to quickly group a Generic List<> by two properties. For the sake of this example lets say I have a List of an Order type with properties of CustomerId, ProductId, and ProductCount. How would I get the average of ProductCounts grouped by CustomerId and ProductId in VB.NET ?

Unfortunatly I cannot do this at DB level (which would have been easy). Also I cannot use LINQ or Lambada as Im on Framewwork 2.0. So I need to do this at application level in VB.net. The list is returned to me sorted by CustomerId and ProductId so I guess with a few loop sI should be able to create a average value for but there must be a cleaner way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:28:32+00:00Added an answer on May 16, 2026 at 10:28 am

    This type of problem is a great example of why LINQ was introduced in C# and VB.NET. In .NET 3.5 and above, LINQ provides the “cleaner way” that you are looking for to solve this problem.

    Unfortunately, because you are using .NET 2.0, you’ll have solve the problem in a more or less “manual” fashion. However, you can still write clean code by encapulating the functionality you are looking for into well-defined classes and methods. This is one (but not the only) of the benefits of LINQ, i.e. that it encapsulates the functionality you expect in a clean, declarative manner.

    Here’s some sample code to get you started:

    'The AggregateItem and AggregateItems classes will help encapsulate the '
    'the functionality you are looking for.'
    
    Public Class AggregateItem
        Public Property GroupByProperty1 As Integer ' Get/Set code...'
        Public Property GroupByProperty2 As Integer ' Get/Set code...'
        Public Property Values As List(Of Double) = New List(Of Double()() _
            ' Get/Set code...'
    
        Public Function GetAverage() As Double
            'Code to calculate and return Average...'
        End Function     
    End Class
    
    Public Class AggregateItems
        Public Property AggregateItemList As List(Of AggregateItem) = _
            New List(Of AggregateItem)() ' Get/Set code...'
    
        Public Sub InsertAggregateItem(groupByProperty1 As Integer, _
                                       groupByProperty2 As Integer, _
                                       value As Double)
            Dim aiExisting As AggregateItem
            aiExisting = GetMatchingAggregateItem(groupByProperty1, _
                                                  groupByProperty2)
            If Not aiExisting Is Nothing Then
                aiExisting.Values.Add(value)
            Else
                aiExisting = New AggregateItem
                aiExisting.GroupByProperty1 = groupByProperty1
                aiExisting.GroupByProperty2 = groupByProperty2
                aiExisting.Values.Add(value)
                AggregateItemList.Add(aiExisting)
        End Sub
    
        Private Function GetMatchingAggregateItem(groupByProperty1 As Integer, _
                                                  groupByProperty2 As Integer) _
                As AggregateItem
             Dim aiMatch As AggregateItem = Nothing
             For Each ag As AggregateItem in AggregateItemList
                 If ag.GroupByProperty1 = groupByProperty1 AndAlso _
                    ag.GroupByProperty2 = groupByProperty2 Then
                     aiMatch = ag
                     Exit For
                 End If
             Next
    
             Return aiMatch
        End Function
    Enc Class
    
    'Then, to consume these classes....'
    
    Public Module MyProgram
        Public Sub Main()
             Dim aItems As New AggregateItems()
             'Say you have List(Of Order) named listOfOrders'
             'We will loop through that list, insert the grouping IDs and values'
             'into our AggregateItems object'
             For Each o As Order In listOfOrders
                aItems.InsertAggregateItem(o.OrderId, o.ProductId, o.ProductCount)
             Next
    
            'Now we can loop through aItems to cleanly get the average: '
            For Each ai As AggregateItem in aItems.AggregateItemsList
                Console.WriteLine("Order: {0} Product: {1} Average: {2}", _
                    ai.GroupByProperty1, ai.GroupByProperty2, _
                    ai.GetAverage())
            Next
        End Sub
    
    End Module
    

    The nice thing about inserting your data into well-encapsulated classes is that the consuming code is very succinct and easy to understand. Also, since you already have your data aggregated into an AggregateItem class, you could easily extend that class with more methods such as GetSum() or GetMax().

    Obviously, you could continue on this path of abstraction to get better re-use out of your code, but I would think that this gives you a good start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.