我正在寻找corect语法来动态地将GroupBy添加到Linq表中,因为可以在用户界面中更改group参数。
基本上我有一个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()})
我喜欢通过表达式树动态添加组,还是有更简单的方法?
感谢您的建议!
答案 0 :(得分: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