?
Solved

Aggregating Objects in Collection

Posted on 2012-03-27
3
Medium Priority
?
289 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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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