Convert LINQ query query results to DataTable

How can I write this query with LINQ in a FoxPro database?

SELECT count(*) FROM Table group by item1

I wrote it as shown below, but it does not work

Dim Query

Dim dt As New DataTable

Dim da = New Odbc.OdbcDataAdapter("SELECT * FROM  table1",connection)
da.Fill(dt)

Query = (From row In dt.AsEnumerable Select row_
          Group By item1 = row.Item(6) Into_       
         count = Count(row.Item(6))).ToList

The following line works:

    Dim q = From p In dt Group p By item = p.Item(6) Into count = Count()

How to link the results of the above query with a GridView? Unfortunately, the installation q, because the DataSource is not working

    grid.DataSource= q

I found that I shoud binds it that way

    Dim table As DataTable = q.ToDataTable()
    DataGridView1.DataSource = table

but i'm so wrong

    'copytodatatable' is not a member of 'System.Collections.Generic.IEnumerable

What does this error refer to?

+3
source share
4 answers

You can skip the DataTable all together and use LINQ to VFP .

+2
source

, , , Lus Oliveira, LINQ DataTable. ()   :

    Public Function EQToDataTable(ByVal parIList As System.Collections.IEnumerable) As System.Data.DataTable
    Dim ret As New System.Data.DataTable()
    Try
        Dim ppi As System.Reflection.PropertyInfo() = Nothing
        If parIList Is Nothing Then Return ret
        For Each itm In parIList
            If ppi Is Nothing Then
                ppi = DirectCast(itm.[GetType](), System.Type).GetProperties()
                For Each pi As System.Reflection.PropertyInfo In ppi
                    Dim colType As System.Type = pi.PropertyType
                    If (colType.IsGenericType) AndAlso
                       (colType.GetGenericTypeDefinition() Is GetType(System.Nullable(Of ))) Then colType = colType.GetGenericArguments()(0)
                    ret.Columns.Add(New System.Data.DataColumn(pi.Name, colType))
                Next
            End If
            Dim dr As System.Data.DataRow = ret.NewRow
            For Each pi As System.Reflection.PropertyInfo In ppi
                dr(pi.Name) = If(pi.GetValue(itm, Nothing) Is Nothing, DBNull.Value, pi.GetValue(itm, Nothing))
            Next
            ret.Rows.Add(dr)
        Next
        For Each c As System.Data.DataColumn In ret.Columns
            c.ColumnName = c.ColumnName.Replace("_", " ")
        Next
    Catch ex As Exception
        ret = New System.Data.DataTable()
    End Try
    Return ret
End Function

:

dim q = [linq query you  write]
Dim dt as DataTable = EQToDataTable(q)
+2

... , ...

select COUNT(*) from Table1

, count...

select COUNT(*) as CountOfRecords from Table1

, ( where)...

int TotalRecords = dt.Rows.Count

... ...

select item, count(*) as TotalPerItem from Table1 group by item

dt.Rows.Count , "ITEMS" ... .

0

Dim table As DataTable = q.ToDataTable() 

Dim table As DataTable = q.ToDataTable().AsEnumerable()

GridView

DataGridView1.DataSource = table    
DataGridView1.DataBind()
0

Source: https://habr.com/ru/post/1755544/


All Articles