Solved

Aggregating Objects in Collection

Posted on 2012-03-27
3
258 Views
Last Modified: 2012-04-03
I have a custom collection that inherits from List(of T) that is used to store objects that expose item no, qty and price properties.

What I want to do is summarise the quantities in a data grid based on item no and price so for example if my objects hold the following information for item no, qty and price:

a, 1, 10.00
a, 1, 10.00
b, 1, 20.00
a, 1, 50.00
a, 1, 10.00

I would show the following in the grid

a, 3, 10.00
a, 1, 50.00
b, 3, 20.00

What's the best way to do this in VB.Net? I've been looking at the Group By in LINQ but most examples are in C# and I'm struggling to get things to work.
0
Comment
Question by:monserob
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
See if this works for you. Please feel free to ask further questions if you require any clarification  = )

Module Module1

    Sub Main()
        Dim aList As New List(Of ObjectsWhichExpose)

        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 10.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 10.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "b", .Quantity = 1, .Price = 20.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 50.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 10.0})

        Dim query = From item In aList _
                    Group item By item.ItemNo, item.Price Into Group _
                    Order By ItemNo _
                    Select New With {.ItemNo = ItemNo, .Quantity = Group.Sum(Function(x As ObjectsWhichExpose) x.Quantity), .Price = Price}

        For Each item In query
            Console.WriteLine("{0}, {1}, {2}", item.ItemNo, item.Quantity, item.Price)
        Next

        Console.ReadKey()
    End Sub

End Module

Public Class ObjectsWhichExpose


    Private _itemNo As String
    Public Property ItemNo() As String
        Get
            Return _itemNo
        End Get
        Set(ByVal value As String)
            _itemNo = value
        End Set
    End Property


    Private _qty As Integer
    Public Property Quantity() As Integer
        Get
            Return _qty
        End Get
        Set(ByVal value As Integer)
            _qty = value
        End Set
    End Property


    Private _price As Double
    Public Property Price() As Double
        Get
            Return _price
        End Get
        Set(ByVal value As Double)
            _price = value
        End Set
    End Property


End Class

Open in new window

0
 

Author Comment

by:monserob
Comment Utility
That's great thanks!

Before I mark as accepted can I ask whether there's an easy of associating the grouped objects with the underlying objects in the collection e.g. if the user was to delete a summary row in the grid I would need to find the individual objects that were grouped on that row and remove them from the collection. Would this be another LINQ to Object?
0
 

Author Comment

by:monserob
Comment Utility
I've not received a response, which is a pity but in fairness a solution to the original question has been provided so I will mark as accepted.

If I run into difficulties with removing the objects that belong to a summarised row I'll post a new question
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now