[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Aggregating Objects in Collection

Posted on 2012-03-27
3
Medium Priority
?
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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