【问题标题】:.GroupBy with .Take VB.Net.GroupBy 和 .Take VB.Net
【发布时间】:2015-09-27 21:57:07
【问题描述】:

我有兴趣根据几个不同的标准过滤一些数据。当我从数据库中提取我的列表时,我目前有两个.Where 子句,一个.GroupBy 和一个.Take。我的问题是我想从每个组中取出 3 个并将其添加到我的列表中,这最终不应该是一个分组列表。

这是我一直在使用的一些代码:

Dim dateRangeEnd As New Date(Property.Year, 10, 31)
Dim configurableCount = 3
Dim unfilteredList = _repository.GetIEnumerableFromDB(param1, param2) _
    .Where(Function(r) ({status1, status2, status3}) _
    .Any(Function(x) r.status.HasValue AndAlso x = r.status.Value)) _
    .Where(Function(r) r.StartDate.Value.Year = Property.Year AndAlso r.StartDate.Value < dateRangeEnd) _
    .GroupBy(Function(r) r.StartDate.Value.Month) _
    .Take(configurableCount)

如何编辑它以便仅将每个月的 configurableCount 添加到我的列表中?

【问题讨论】:

    标签: vb.net ienumerable


    【解决方案1】:

    您使用SelectMany 展平序列序列。

    .Where(Function(r) ({status1, status2, status3}) _
    .Any(Function(x) r.status.HasValue AndAlso x = r.status.Value)) _
    .Where(Function(r) r.StartDate.Value.Year = Property.Year AndAlso r.StartDate.Value < dateRangeEnd) _
    .GroupBy(Function(r) r.StartDate.Value.Month) _
    .SelectMany(Function(g) g.Take(configurableCount))
    

    【讨论】:

    • 而 .SelectMany 会从每个组中提取可配置的数量吗?如果我知道这是因为 (Function(g) g.Take(configurableCount)) 正在从每个组中选择而发生的?
    • SelectMany 将获取选择器函数返回的所有内容。选择器函数对外部序列(即组的序列)中的每个元素调用一次,因此它对每个组分别调用Take
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2018-07-17
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多