0

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

基本上我有一个 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()})

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

谢谢你的建议!

4

1 回答 1

0

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

    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

于 2012-08-29T10:24:56.550 回答