以 属性 作为参数将 LINQ 查询放入方法中

Put LINQ Query in Method with Property as Parameter

我不知道如何在标题中解释,所以我尽可能详细地描述我的问题:

我有一个具有不同属性的 object 列表,我通过名为“OriginID”的 属性 对它们进行分组,并使用 LINQ 查询来获取不同属性(NetTotal 和 ShippingCost)的总和。

由于我有多个属性需要求和,我现在需要为每个组元素复制 LINQ 查询(我有更多字段,例如“OriginID”,例如 CountryID 等等。

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)})

我想将“一个”查询封装到一个函数中,其中组 - 即 object 的 属性 - 可以作为参数给出,如下所示:

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

甚至可以这样做吗?

我添加更多细节:

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

一个简单的解决方案是包含一个 select 案例:

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)})

编辑#1:

这是我试过的:

  ....
    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

输出: