-
Feb 24th, 2012, 03:05 AM
#1
Thread Starter
Member
Sumproduct with LINQ
Can I calculate sumproduct by applying LINQ to List collection?
For example, if I have a list of my custom object with four items as:
Value Percentage Category
1000 6,0% A
3000 5,0% B
2000 7,0% A
4000 5,5% B
How to return the weighted Percentage for each category with LINQ? Weight is the Value.
-
Feb 24th, 2012, 08:47 AM
#2
Re: Sumproduct with LINQ
do you mean like this:
vb Code:
Public Class Form1
Dim myList As New List(Of customObject)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myList.Add(New customObject(1000, 6D, "A"))
myList.Add(New customObject(3000, 5D, "B"))
myList.Add(New customObject(2000, 7D, "A"))
myList.Add(New customObject(4000, 5.5D, "B"))
Dim groups = (From co In myList _
Group co By category = co.Category Into Group _
Select New With { _
.Category = category, _
.weightSum = Group.Sum(Function(g) g.Value), _
.percentageSum = Group.Sum(Function(g) g.Percentage)} _
).ToList
End Sub
End Class
vb Code:
Public Class customObject
Public Value As Integer
Public Percentage As Decimal
Public Category As String
Public Sub New(ByVal Value As Integer, ByVal Percentage As Decimal, ByVal Category As String)
Me.Value = Value
Me.Percentage = Percentage
Me.Category = Category
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 24th, 2012, 09:38 AM
#3
Re: Sumproduct with LINQ
or did you mean this?:
vb Code:
Public Class Form1
Dim myList As New List(Of customObject)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myList.Add(New customObject(1000, 6D, "A"))
myList.Add(New customObject(3000, 5D, "B"))
myList.Add(New customObject(2000, 7D, "A"))
myList.Add(New customObject(4000, 5.5D, "B"))
Dim groups = From co In myList _
Select New With { _
.Category = co.Category, _
.Product = co.Value * (co.Percentage / 100)}
Dim groupSums = (From g In groups _
Group g By category = g.Category Into Group _
Select New With { _
.Category = category, _
.productSum = Group.Sum(Function(g) g.Product)} _
).ToList
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 28th, 2012, 04:08 AM
#4
Thread Starter
Member
Re: Sumproduct with LINQ
Originally Posted by .paul.
or did you mean this?:
vb Code:
Public Class Form1 Dim myList As New List(Of customObject) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myList.Add(New customObject(1000, 6D, "A")) myList.Add(New customObject(3000, 5D, "B")) myList.Add(New customObject(2000, 7D, "A")) myList.Add(New customObject(4000, 5.5D, "B")) Dim groups = From co In myList _ Select New With { _ .Category = co.Category, _ .Product = co.Value * (co.Percentage / 100)} Dim groupSums = (From g In groups _ Group g By category = g.Category Into Group _ Select New With { _ .Category = category, _ .productSum = Group.Sum(Function(g) g.Product)} _ ).ToList End Sub End Class
This one seems to get it right.
How would that be extended to divide the productSum with each category's total value (to get the weighted Percentage) ?
So with the above we get:
Category productSum
A 200
B 370
I would further need to get:
Category WeightedPercentage
A 0.0666667 -> (i.e. 200 divided by 3000)
B 0.0528571 -> (i.e. 370 divided by 7000)
Last edited by RK62; Feb 28th, 2012 at 05:24 AM.
-
Feb 28th, 2012, 09:10 AM
#5
Re: Sumproduct with LINQ
try this:
vb Code:
Public Class Form1
Dim myList As New List(Of customObject)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myList.Add(New customObject(1000, 6D, "A"))
myList.Add(New customObject(3000, 5D, "B"))
myList.Add(New customObject(2000, 7D, "A"))
myList.Add(New customObject(4000, 5.5D, "B"))
Dim groups = From co In myList _
Select New With { _
.Category = co.Category, _
.Product = co.Value * (co.Percentage / 100), _
.Value = co.Value}
Dim groupSums = (From g In groups _
Group g By category = g.Category Into Group _
Select New With { _
.Category = category, _
.productSum = Group.Sum(Function(g) g.Product), _
.WeightedPercentage = .productSum / Group.Sum(Function(g) g.Value)} _
).ToList
End Sub
End Class
Public Class customObject
Public Value As Integer
Public Percentage As Decimal
Public Category As String
Public Sub New(ByVal Value As Integer, ByVal Percentage As Decimal, ByVal Category As String)
Me.Value = Value
Me.Percentage = Percentage
Me.Category = Category
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 29th, 2012, 02:25 AM
#6
Thread Starter
Member
Re: Sumproduct with LINQ
Originally Posted by .paul.
try this:
vb Code:
Public Class Form1
Dim myList As New List(Of customObject)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myList.Add(New customObject(1000, 6D, "A"))
myList.Add(New customObject(3000, 5D, "B"))
myList.Add(New customObject(2000, 7D, "A"))
myList.Add(New customObject(4000, 5.5D, "B"))
Dim groups = From co In myList _
Select New With { _
.Category = co.Category, _
.Product = co.Value * (co.Percentage / 100), _
.Value = co.Value}
Dim groupSums = (From g In groups _
Group g By category = g.Category Into Group _
Select New With { _
.Category = category, _
.productSum = Group.Sum(Function(g) g.Product), _
.WeightedPercentage = .productSum / Group.Sum(Function(g) g.Value)} _
).ToList
End Sub
End Class
Public Class customObject
Public Value As Integer
Public Percentage As Decimal
Public Category As String
Public Sub New(ByVal Value As Integer, ByVal Percentage As Decimal, ByVal Category As String)
Me.Value = Value
Me.Percentage = Percentage
Me.Category = Category
End Sub
End Class
Perfect! Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|