locked
Find unique values in list collection and perform further processing RRS feed

  • Question

  • User1909155429 posted
    
    
    
    
    For Each p As freelancerproduct In pr
    
     parameters.Add(New String() {"amount", p.Rate.ToString()})
    
     parameters.Add(New String() {"email", p.SellerPaypalAccountId})
    
    
    counter += 1
    
    
    Next
    
    
    The problem here is that  SellerPaypalAccountId  property has to be a unique element before  added to the parameters list. As a consequence each p element in the list needs to be checked for repetitive  p.SellerPaypalAccountId property.
    
    if more than one p.SellerPaypalAccountId value in the list  then each corresponding  p.Rate property value needs to be added together before the  p.SellerPaypalAccountId and p.Rate elements are added to parameters. shown below. 

    Sunday, August 18, 2013 8:15 AM

Answers

  • User-417640953 posted

    Hello peterthegreat,

    I'm glad to you feedback. For the above Linq query , I was use C#.

    Below demo is pure VB code, please try it.

    Product.vb

    Public Class Product
        Public Sub New()
        End Sub
        Public Sub New(rate As Single, sellPAcc As String)
            Me.Rate = rate
            Me.SellerPaypalAccountId = sellPAcc
        End Sub
        Public Property Rate() As Decimal
            Get
                Return m_Rate
            End Get
            Set(value As Decimal)
                m_Rate = value
            End Set
        End Property
        Private m_Rate As Decimal
     
        Public Property SellerPaypalAccountId() As String
            Get
                Return m_SellerPaypalAccountId
            End Get
            Set(value As String)
                m_SellerPaypalAccountId = Value
            End Set
        End Property
        Private m_SellerPaypalAccountId As String
    End Class
    

    aspx.vb

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            
    
            Dim products As New List(Of Product)()
            products.AddRange(New Product() {New Product(3.45, "accountid1"), New Product(2.45, "accountid1"), New Product(5.15, "accountid2"), New Product(6.25, "accountid2"), New Product(1.45, "accountid3"), New Product(0.95, "accountid3")})
             
            Dim pr = From p In products Group p.Rate By Key = p.SellerPaypalAccountId Into Group = Group _
                      Select New With {.SellerPaypalAccountId = Key, .Rate = Group.Sum()}
            
            Dim parameters As New List(Of String())()
    
            For Each p In pr
                Response.Write(Convert.ToString(p.SellerPaypalAccountId) & ":" & Convert.ToString(p.Rate) & "<br/>")
                parameters.Add(New String() {"amount", p.Rate.ToString()})
                parameters.Add(New String() {"email", p.SellerPaypalAccountId})
            Next
    
    
    
        End Sub

    I test it runs fine, thanks.

     

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2013 10:40 AM

All replies

  • User-417640953 posted

    Hello,

    I overviewed your description and code you provided. And I see that you want get Rate together which has same SellerPaypalAccountId .

    later, add the list to parameters list.

    For the issue, I suggest you use linq query to complete the efficacy. Please try below code:

    Dim pr2 = (From s In From c In prGroup c By c.SellerPaypalAccountIdNew With { _
             Key .Rate = s.Sum(Function(c) CSng(c.Rate)), _
             Key .SellerPaypalAccountId = s.Key _
    }).ToList()
    
    Dim parameters As New List(Of String())()
    
    For Each p As var In pr2
             parameters.Add(New String() {"amount", p.Rate.ToString()})
             parameters.Add(New String() {"email", p.SellerPaypalAccountId.ToString()})
             
    Next
    

    Hope it helps, thanks.

     

    Best Regards!

    Monday, August 19, 2013 3:45 AM
  • User1909155429 posted

    That is a very interesting method!

    Although i am working with pure vb code in class file. With data stored as a Product object in a List collection.

     

    Thanks

    Monday, August 19, 2013 8:21 AM
  • User1909155429 posted

    i am getting an error message on c stating '(' is missing ?

    Also how is the query reference the product object amount and email values using c as an argument  from list collection ?

     

    pr is the reference variable for product object.

     

    Thanks

     

    Monday, August 19, 2013 7:27 PM
  • User-417640953 posted

    Hello peterthegreat,

    I'm glad to you feedback. For the above Linq query , I was use C#.

    Below demo is pure VB code, please try it.

    Product.vb

    Public Class Product
        Public Sub New()
        End Sub
        Public Sub New(rate As Single, sellPAcc As String)
            Me.Rate = rate
            Me.SellerPaypalAccountId = sellPAcc
        End Sub
        Public Property Rate() As Decimal
            Get
                Return m_Rate
            End Get
            Set(value As Decimal)
                m_Rate = value
            End Set
        End Property
        Private m_Rate As Decimal
     
        Public Property SellerPaypalAccountId() As String
            Get
                Return m_SellerPaypalAccountId
            End Get
            Set(value As String)
                m_SellerPaypalAccountId = Value
            End Set
        End Property
        Private m_SellerPaypalAccountId As String
    End Class
    

    aspx.vb

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            
    
            Dim products As New List(Of Product)()
            products.AddRange(New Product() {New Product(3.45, "accountid1"), New Product(2.45, "accountid1"), New Product(5.15, "accountid2"), New Product(6.25, "accountid2"), New Product(1.45, "accountid3"), New Product(0.95, "accountid3")})
             
            Dim pr = From p In products Group p.Rate By Key = p.SellerPaypalAccountId Into Group = Group _
                      Select New With {.SellerPaypalAccountId = Key, .Rate = Group.Sum()}
            
            Dim parameters As New List(Of String())()
    
            For Each p In pr
                Response.Write(Convert.ToString(p.SellerPaypalAccountId) & ":" & Convert.ToString(p.Rate) & "<br/>")
                parameters.Add(New String() {"amount", p.Rate.ToString()})
                parameters.Add(New String() {"email", p.SellerPaypalAccountId})
            Next
    
    
    
        End Sub

    I test it runs fine, thanks.

     

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2013 10:40 AM
  • User1909155429 posted

    I found this method although it corresponds to database. Is this not a alternative workable solution for the aforementioned task ?

      'Dim averageCustomersByCity = From cust In db.Customers
            '                     Group By cust.City
            '                     Into Sum(cust.Orders.Count)
            '                     Order By Average
    

    Tuesday, August 20, 2013 11:47 AM