Home > database >  Put LINQ Query in Method with Property as Parameter
Put LINQ Query in Method with Property as Parameter

Time:03-19

I didnt know how to explain in the title, so I describe my concern as detailed as possible:

I have a list of object with different properties and I group them via a property named "OriginID" with a LINQ query to get a sum of different properties (NetTotal and ShippingCost).

As I have multiple properties to get the sum of, I now need to copy the LINQ Query for each group element (I have more field like "OriginID" e.g. CountryID and so on.

Dim ListA = SalesTurnoverResult.GroupBy(
    Function(x) x.OriginID).Select(
    Function(a) New With {
        Key .OriginID = a.Key,
        Key .sum_a = a.Sum(Function(s) s.NetTotal),
        Key .sum_b = a.Sum(Function(s) s.ShippingCost)})

I would like to encapsule the "one" query into a function where the group - which is the property of the object - can be given as parameter like so:

Public SubGetGroupedSummary(NameOfProperty As .... [the property])
    Dim ListA = SalesTurnoverResult.GroupBy(
        Function(x) x.[NameOfProperty]).Select(
        Function(a) New With {
            Key .OriginID = a.Key,
            Key .sum_a = a.Sum(Function(s) s.NetTotal),
            Key .sum_b = a.Sum(Function(s) s.ShippingCost)})
End Sub

Is it even possible to do so?

I add some more details:

Public SalesTurnoverResult As List(Of SummaryClass)

'The (Sample)Class
Public Class SummaryClass
    Public Property NetTotal As Single'Sum
    Public Property ShippingCost As Single'Sum

    Public Property InvoiceCountry As Integer'Group
    Public Property OriginID As Single'Group
End Class

CodePudding user response:

A simple solution would be to include a select case:

ListA = SalesTurnoverResult.GroupBy(
    Function(x)
        Select Case NameOfProperty
            Case "OriginID" : Return x.OriginID
            Case "InvoiceCountry" : Return x.InvoiceCountry
        End Select
    End Function
    ).Select(
    Function(a) New With {
    Key .OriginID = a.Key,
    Key .sum_a = a.Sum(Function(s) s.NetTotal),
    Key .sum_b = a.Sum(Function(s) s.ShippingCost)})

Edit #1:

This is what I have tried:

  ....
    With SalesTurnoverResult
        Dim sc As New SummaryClass
        sc.NetTotal = 100 : sc.ShippingCost = 20 : sc.InvoiceCountry = 1 : sc.OriginID = 1000 : sc.Str = "abc"
        .Add(sc)
        sc = New SummaryClass
        sc.NetTotal = 50 : sc.ShippingCost = 25 : sc.InvoiceCountry = 1 : sc.OriginID = 1001 : sc.Str = "efg"
        .Add(sc)
        sc = New SummaryClass
        sc.NetTotal = 120 : sc.ShippingCost = 25 : sc.InvoiceCountry = 2 : sc.OriginID = 1002 : sc.Str = "jkl"
        .Add(sc)
        sc = New SummaryClass
        sc.NetTotal = 50 : sc.ShippingCost = 20 : sc.InvoiceCountry = 3 : sc.OriginID = 1001 : sc.Str = "abc"
        .Add(sc)
    End With
    GetGroupedSummaryStrictON("OriginID")
    GetGroupedSummaryStrictON("InvoiceCountry")
    GetGroupedSummaryStrictON("Str")
   ....
   ....

Public Sub GetGroupedSummaryStrictON(NameOfProperty As String)
    Try
        Dim ListA = SalesTurnoverResult.GroupBy(
        Function(x) As Object
            Select Case NameOfProperty
                Case "OriginID" : Return CSng(x.OriginID)
                Case "InvoiceCountry" : Return CInt(x.InvoiceCountry)
                Case Else  ' x.Str
            End Select
            Return CType(x.Str, String)
        End Function).Select(
            Function(a) New With {
            Key .OriginID = a.Key,
            Key .sum_a = a.Sum(Function(s) s.NetTotal),
            Key .sum_b = a.Sum(Function(s) s.ShippingCost)}).ToArray

        Console.WriteLine(NameOfProperty)
        For Each a In ListA
            Console.WriteLine(String.Format("{0} {1} {2}", a.OriginID, a.sum_a, a.sum_b))
        Next
        Console.WriteLine("--------------------")

    Catch ex As Exception
    End Try
End Sub

The output:

enter image description here

  • Related