如何使用LINQ将此查询写入FoxPro数据库?
SELECT count(*) FROM Table group by item1
我写的如下,但它不起作用
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
以下行有效:
Dim q = From p In dt Group p By item = p.Item(6) Into count = Count()
如何将上述查询的结果绑定到GridView?不幸的是,将q
设置为DataSource不起作用
grid.DataSource= q
我发现我应该这样绑定它
Dim table As DataTable = q.ToDataTable()
DataGridView1.DataSource = table
但我和这样的错误
'copytodatatable' is not a member of 'System.Collections.Generic.IEnumerable
这个错误是指什么?
答案 0 :(得分:2)
答案 1 :(得分:2)
我搜索了几天,终于遇到了由Lus Oliveira编写的这个功能,它将LINQ结果转换为DataTable。 (Article) 实际功能:
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 :(得分:0)
你的查询正在拉下每条记录...如果你想要计数,你可以做...
select COUNT(*) from Table1
或者,要明确地将结果列命名为计数...
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”...然后,您可以滚动每个项目的记录并分别对该项目进行计数。
答案 3 :(得分:0)
更改以下行
Dim table As DataTable = q.ToDataTable()
到
Dim table As DataTable = q.ToDataTable().AsEnumerable()
然后你可以尝试将它绑定到GridView
DataGridView1.DataSource = table
DataGridView1.DataBind()