使用Linq count函数和vb.net datatable

时间:2011-08-02 16:42:45

标签: vb.net linq datatable

我写下面的代码。我知道这很愚蠢。 我想使用linq计数做空。 请给我点亮。

            Dim query0 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") = 0
            Dim count0 As Integer = query0.Count

            Dim query1 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") = 1
            Dim count1 As Integer = query1.Count

            Dim query2 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") = 1
            Dim count2 As Integer = query2.Count


            Dim query3 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") = 3
            Dim count3 As Integer = query3.Count

            Dim query4 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") = 4
            Dim count4 As Integer = query4.Count

            Dim query5 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") = 5
            Dim count5 As Integer = query5.Count

            Dim queryOver6 = From obj In dtAginglist _
                        Where (obj.Field(Of String)("CurrentStatus") = "Open" Or obj.Field(Of String)("CurrentStatus") = "Acknowledge") _
                          And obj.Field(Of Integer)("OpenDays") > 5
            Dim countOver6 As Integer = queryOver6.Count

            Dim Result As String
            Result = String.Format("0 Day : {1}{0}1 Day :{2}{0}2 Days :{3}{0}3 Days :{3}{0}4 Days :{5}{0}5 Days :{6}{0}Over 6 Days :{7}{0}", _
                                   vbCrLf, count0, count1, count2, count3, count4, count5)

我尝试使用下面的代码,但如果source是数据表的话它就不能使用。

Dim orderCounts = From c In customers New With { _
    c.CustomerID, Key .OrderCount = c.Orders.Count() }

1 个答案:

答案 0 :(得分:1)

我相信您需要使用AsQueryable()扩展来执行此操作:

Dim orderCounts = From c In customers.AsQueryable()
                  New With
                  {
                      c.CustomerID,
                      Key.OrderCount = c.Orders.Count()
                  }

您还需要添加导入System.Data.DataSetExtensions

原因是DataTable的Rows集合没有实现IEnumerable。见LINQ query on a DataTable