VB.Net通过表达式树添加GroupBy



我正在寻找将GroupBy添加到Linq表动态中的核心语法,因为组参数可以在用户界面中更改。

基本上我有一个林克。EntitySet,我想为可变参数添加GroupBy

天,周月年度

而不是像那样写4个查询

data.View.GroupBy(Function(x) x.Moment.Date).OrderBy(Function(x) x.Key).Select(Function(x) New With {.Date = x.Key, .Count = x.Count()})
data.View.GroupBy(Function(x) x.Moment.Week).OrderBy(Function(x) x.Key).Select(Function(x) New With {.Date = x.Key, .Count = x.Count()})
data.View.GroupBy(Function(x) x.Moment.Month).OrderBy(Function(x) x.Key).Select(Function(x) New With {.Date = x.Key, .Count = x.Count()})
data.View.GroupBy(Function(x) x.Moment.Year).OrderBy(Function(x) x.Key).Select(Function(x) New With {.Date = x.Key, .Count = x.Count()})

我喜欢通过表达式树动态添加组,或者有更简单的方法吗?

谢谢你的建议!

令人惊讶的是,它比我想象的要简单!

    Private Function bar(x As Date) As Object
        Select Case ddlStatRange.SelectedValue
            Case 1
                Return x.Month
            Case 2
                Return x.Month
            Case 3
                Return x.Year
            Case Else
                Return x.Date
        End Select
    End Function
    Dim foo = data.View.GroupBy(Function(x) bar(x.Moment)).OrderBy(Function(x) x.Key).Select(Function(x) New With {.Date = x.Key, .Count = x.Count()}) 

无论如何,如果有人有更好的想法:D

最新更新