Solved

Aggregating Objects in Collection

Posted on 2012-03-27
3
268 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37771451
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
ID: 37771959
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
ID: 37801766
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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