我想獲得由LINQ查詢返回的項目的計數。我會認爲它是.Count但顯然不是。 我正在從Excel中加載DataTable,然後根據標識符獲取不同的行。我現在正在嘗試計算返回的項目數量。 Leases
包含我希望計數的行的數據。我嘗試了一些東西(現在已經註釋掉了),但都沒有成功。如何獲得由LINQ查詢返回的項目的計數
也許一個LINQ專家可以幫助我?
這裏是我的代碼:
Dim Leases As Object
Dim dtData As System.Data.DataTable = LoadExcelAsDb(oFile.FullName)
Try
' How many leases are there
Leases = From row In dtData.AsEnumerable() Select row.Field(Of Object)("F2") Distinct
' LeaseCt = New System.Linq.SystemCore_EnumerableDebugView(Of Object)(Leases).Items.count
' LeaseCt = New System.Linq.Enumerable(Of Object)(Leases).Items.count
' LeaseCt = Leases.Count
Catch ex As Exception
Debug.WriteLine("Error reading files" + ex.ToString)
End Try
''' <summary>
''' Loads an excel file as a datatable
''' </summary>
''' <param name="sFilename">Filename of excel to be loaded </param>
''' <returns>Populated Datatable</returns>
Private Function LoadExcelAsDb(sFilename As String) As System.Data.DataTable
' Connect to Excel get table
Dim oConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilename + " ; Extended Properties=Excel 12.0;")
Dim oCommand As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", oConnection)
Dim DtSet As System.Data.DataTable = New System.Data.DataTable
Try
oCommand.Fill(DtSet)
Catch ex As Exception
Debug.WriteLine("Error reading files" + ex.ToString)
End Try
Return DtSet
End Function
嘗試使用此鏈接http:// stackoverflow。com/questions/3153641/linq-count-best-method –
'租約'將成爲「內存查詢」 - 它不會有一個計數,直到你執行它。 ''LeaseCt =(Leases).Count()'會給你計數* that *查詢 – Plutonix
'LeaseCt = Leases.ToList()。Count()' –