【问题标题】:Use LINQ to Group data from data table使用 LINQ 对数据表中的数据进行分组
【发布时间】:2021-01-03 00:23:09
【问题描述】:

我有一个 linq 问题。我想使用 LINQ 对 DataTable 中的数据进行分组(列:RUN_NAME、H_YYYY、H_MM、H_MON)。

数据如下:

RUN_NAME H_YYYY H_MM H_MON
2019-1 2019 1 Jan
2019-1 2019 2 Feb
2019-1 2019 3 Mar
2019-1 2019 4 Apr
2019-2 2019 5 May
2019-2 2019 6 Jun
2019-2 2019 7 Jul
2019-2 2019 8 Aug
2019-3 2019 9 Sep
2019-3 2019 10 Oct
2019-3 2019 11 Nov
2019-3 2019 12 Dec

我需要这样的结果,其中将单个运行详细信息分组为每次运行的一部分(2019-1、2019-2、2019-3):

2019-1

  2019 1 Jan

  2019 2 Feb

  2019 3 Mar

  2019 4 Apr

2019-2

  2019 5 May

  2019 6 Jun

  2019 7 Jul

  2019 8 Aug

2019-3

  2019 9 Sep

  2019 10 Oct

  2019 11 Nov

  2019 12 Dec

这是我迄今为止尝试过的:

    CalendarMonthList = (From rw As DataRow In dt.Rows Select New CalendarMonth With {
                                                                                  .Run_Name=CheckStringNull(rw("run_name").ToString),
.Month_Scope = (From r In dt.AsEnumerable Where r("run_name") = .Run_Name Select New MonthScope With {.h_yyyy = CheckDbNull(rw("h_yyyy")), 
.h_mm=CheckDbNull(rw("h_mm")),
.h_mon=CheckStringNull(rw("h_mon").ToString)}).Distinct.ToList()}).Distinct.ToList()

我对 CalendarMonth 的定义如下:

Public Class CalendarMonth
        Public Property Run_Name As String
        Public Property Month_Scope As List(Of MonthScope)
End Class

我对 MonthScope 类的定义如下:

Public Class MonthScope
        Public Property h_yyyy As Integer?
        Public Property h_mm As Integer?
        Public Property h_mon As String
End Class

【问题讨论】:

  • 那么实际的问题是什么?您的代码提供了什么结果,这与您的预期有何不同?
  • 您真的要分组还是只是想排序?我没有看到任何尝试在您的代码中进行分组的证据,因此显然不会发生分组。如果你真的想分组,那么你应该阅读如何使用 LINQ 进行分组,然后尝试实现你所学的,如果它不起作用,然后发布一个问题。
  • 有一个分组示例here 可以帮助您入门。那是搜索“vb.net linq group”的结果第一页的一半。

标签: vb.net linq grouping


【解决方案1】:

所以我认为你的“我需要这样的东西”是你想要的对象的表示; 3 个 CalendarMonth,每个都有一个填充的 Month_Scope 列表,它们是 MonthScopes(其中 4 个)

首先,我真的很想稍微调整一下你的类,一个是删除不可靠的属性名称,另一个是添加几个构造函数,它们将从数据行构造类。如果您愿意,您可以以其他方式构造它(例如,共享 FromDataRow 方法或工厂),或者如果您愿意,您可以将此逻辑合并回您的 LINQ 语句中。我将其作为构造函数进行,因为我觉得它可以保持它更清洁和开启-指向它:

Public Class CalendarMonth
    Public Property RunName As String
    Public Property MonthScopes As New List(Of MonthScope)

    Public Sub New(rn As String, ms As IEnumerable(Of DataRow))
        RunName = rn
        MonthScopes = ms.Select(Function(msro) New MonthScope(msro)).ToList()
    End Sub
End Class

Public Class MonthScope
    Public Property Yyyy As Integer?
    Public Property MM As Integer?
    Public Property Mon As String

    Public Sub New(ms As DataRow)
        If Not ms.IsNull("h_yyyy") Then Yyyy = CInt(ms("h_yyyy"))
        If Not ms.IsNull("h_mm") Then MM = CInt(ms("h_mm"))
        If Not ms.IsNull("h_mon") Then Mon = CStr(ms("h_mon"))
    End Sub
End Class

然后你有一个 LINQ 语句,它更好:

    Dim calendarMonthList = dt.AsEnumerable() _
        .GroupBy(Function(r) CStr(r("run_name"))) _
        .Select(Function(g) New CalendarMonth(g.Key, g)) _
        .ToList()

【讨论】:

  • 完美!这正是我一直在寻找的。谢谢你。非常感激。 LINQ 新手在这里。
猜你喜欢
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 2016-06-28
  • 2011-09-02
相关资源
最近更新 更多