我正在尝试从linq查询返回数据表但收到错误消息。我在VS2012中使用.net framework 4.0。
directions.src = "ball.gif";
'----------------------------'
<Table>
Public Class StaffOfficeAccess
<Column(CanBeNull:=False, IsPrimaryKey:=True, IsDbGenerated:=True)>
Public Property StaffOfficeAccessID As Int32 = 0
<Column>
Public Property StaffID As Int32 = 0
<Column>
Public Property OfficeID As Int32 = 0
<Column(IsVersion:=True, IsDbGenerated:=True)>
Public Property Version As Byte()
End Class
'-------错误----------'
无法将类型为'd__61 Public Function GetByStaffID(ByVal staffID As Int32) As DataTable
Dim query As IEnumerable(Of DataRow) = CType((From oa In db.StaffOfficeAccess.AsEnumerable() _
Join o In db.Office.AsEnumerable() On oa.OfficeID Equals o.OfficeID _
Select oa.OfficeID,
o.OfficeName), Global.System.Collections.Generic.IEnumerable(Of Global.System.Data.DataRow))
Dim table As DataTable = System.Data.DataTableExtensions.CopyToDataTable(query)
Return table
End Function
2 [System.Int32,System.String]]'的对象强制转换为'System.Collections.Generic.IEnumerable`1 [System.Data.DataRow] ”。
我在这里尝试过这个例子https://msdn.microsoft.com/en-us/library/bb396189%28v=vs.110%29.aspx但没有运气。我在VS2012中没有得到CopyToDataTable。
答案 0 :(得分:1)
使用Aggregate
扩展方法MSDN
Dim dt As New DataTable()
dt.Columns.Add("OfficeID", GetType(Integer))
dt.Columns.Add("OfficeName", GetType(String))
Dim query = From oa In db.StaffOfficeAccess.AsEnumerable()
Join o In db.Office.AsEnumerable()
On oa.OfficeID Equals o.OfficeID
Select oa.OfficeID, o.OfficeName
query.Aggregate(Of DataTable)(dt,
Function(dtb, o)
Dim dr As DataRow = dtb.NewRow()
dr.SetField("OfficeID", o.OfficeID)
dr.SetField("OfficeName", o.OfficeName)
dtb.Rows.Add(dr)
Return dtb
End Function)