Results 1 to 6 of 6

Thread: Sumproduct with LINQ

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    42

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Sumproduct with LINQ

    do you mean like this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim myList As New List(Of customObject)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         myList.Add(New customObject(1000, 6D, "A"))
    7.         myList.Add(New customObject(3000, 5D, "B"))
    8.         myList.Add(New customObject(2000, 7D, "A"))
    9.         myList.Add(New customObject(4000, 5.5D, "B"))
    10.  
    11.         Dim groups = (From co In myList _
    12.                      Group co By category = co.Category Into Group _
    13.                      Select New With { _
    14.                      .Category = category, _
    15.                      .weightSum = Group.Sum(Function(g) g.Value), _
    16.                      .percentageSum = Group.Sum(Function(g) g.Percentage)} _
    17.                      ).ToList
    18.  
    19.     End Sub
    20. End Class
    vb Code:
    1. Public Class customObject
    2.     Public Value As Integer
    3.     Public Percentage As Decimal
    4.     Public Category As String
    5.  
    6.     Public Sub New(ByVal Value As Integer, ByVal Percentage As Decimal, ByVal Category As String)
    7.         Me.Value = Value
    8.         Me.Percentage = Percentage
    9.         Me.Category = Category
    10.     End Sub
    11.  
    12. End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Sumproduct with LINQ

    or did you mean this?:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim myList As New List(Of customObject)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         myList.Add(New customObject(1000, 6D, "A"))
    7.         myList.Add(New customObject(3000, 5D, "B"))
    8.         myList.Add(New customObject(2000, 7D, "A"))
    9.         myList.Add(New customObject(4000, 5.5D, "B"))
    10.  
    11.         Dim groups = From co In myList _
    12.                      Select New With { _
    13.                      .Category = co.Category, _
    14.                      .Product = co.Value * (co.Percentage / 100)}
    15.  
    16.         Dim groupSums = (From g In groups _
    17.                          Group g By category = g.Category Into Group _
    18.                          Select New With { _
    19.                          .Category = category, _
    20.                          .productSum = Group.Sum(Function(g) g.Product)} _
    21.                          ).ToList
    22.  
    23.     End Sub
    24. End Class

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    42

    Re: Sumproduct with LINQ

    Quote Originally Posted by .paul. View Post
    or did you mean this?:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim myList As New List(Of customObject)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         myList.Add(New customObject(1000, 6D, "A"))
    7.         myList.Add(New customObject(3000, 5D, "B"))
    8.         myList.Add(New customObject(2000, 7D, "A"))
    9.         myList.Add(New customObject(4000, 5.5D, "B"))
    10.  
    11.         Dim groups = From co In myList _
    12.                      Select New With { _
    13.                      .Category = co.Category, _
    14.                      .Product = co.Value * (co.Percentage / 100)}
    15.  
    16.         Dim groupSums = (From g In groups _
    17.                          Group g By category = g.Category Into Group _
    18.                          Select New With { _
    19.                          .Category = category, _
    20.                          .productSum = Group.Sum(Function(g) g.Product)} _
    21.                          ).ToList
    22.  
    23.     End Sub
    24. 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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Sumproduct with LINQ

    try this:

    vb Code:
    1. Public Class Form1
    2.     Dim myList As New List(Of customObject)
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         myList.Add(New customObject(1000, 6D, "A"))
    5.         myList.Add(New customObject(3000, 5D, "B"))
    6.         myList.Add(New customObject(2000, 7D, "A"))
    7.         myList.Add(New customObject(4000, 5.5D, "B"))
    8.         Dim groups = From co In myList _
    9.         Select New With { _
    10.         .Category = co.Category, _
    11.         .Product = co.Value * (co.Percentage / 100), _
    12.         .Value = co.Value}
    13.  
    14.         Dim groupSums = (From g In groups _
    15.         Group g By category = g.Category Into Group _
    16.         Select New With { _
    17.         .Category = category, _
    18.         .productSum = Group.Sum(Function(g) g.Product), _
    19.         .WeightedPercentage = .productSum / Group.Sum(Function(g) g.Value)} _
    20.         ).ToList
    21.        
    22.     End Sub
    23. End Class
    24.  
    25. Public Class customObject
    26.     Public Value As Integer
    27.     Public Percentage As Decimal
    28.     Public Category As String
    29.  
    30.     Public Sub New(ByVal Value As Integer, ByVal Percentage As Decimal, ByVal Category As String)
    31.         Me.Value = Value
    32.         Me.Percentage = Percentage
    33.         Me.Category = Category
    34.     End Sub
    35.  
    36. End Class

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    42

    Re: Sumproduct with LINQ

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Public Class Form1
    2.     Dim myList As New List(Of customObject)
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         myList.Add(New customObject(1000, 6D, "A"))
    5.         myList.Add(New customObject(3000, 5D, "B"))
    6.         myList.Add(New customObject(2000, 7D, "A"))
    7.         myList.Add(New customObject(4000, 5.5D, "B"))
    8.         Dim groups = From co In myList _
    9.         Select New With { _
    10.         .Category = co.Category, _
    11.         .Product = co.Value * (co.Percentage / 100), _
    12.         .Value = co.Value}
    13.  
    14.         Dim groupSums = (From g In groups _
    15.         Group g By category = g.Category Into Group _
    16.         Select New With { _
    17.         .Category = category, _
    18.         .productSum = Group.Sum(Function(g) g.Product), _
    19.         .WeightedPercentage = .productSum / Group.Sum(Function(g) g.Value)} _
    20.         ).ToList
    21.        
    22.     End Sub
    23. End Class
    24.  
    25. Public Class customObject
    26.     Public Value As Integer
    27.     Public Percentage As Decimal
    28.     Public Category As String
    29.  
    30.     Public Sub New(ByVal Value As Integer, ByVal Percentage As Decimal, ByVal Category As String)
    31.         Me.Value = Value
    32.         Me.Percentage = Percentage
    33.         Me.Category = Category
    34.     End Sub
    35.  
    36. 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
  •  



Click Here to Expand Forum to Full Width