【发布时间】:2015-10-09 13:46:36
【问题描述】:
我正在尝试从 linq 查询返回数据表,但收到错误消息。我在VS2012中使用.net framework 4.0。
<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
'----------------'
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
'-------错误---------'
无法将类型为“d__614[staff.Objects.StaffOfficeAccess,AMIS.Objects.Office,System.Int32,VB$AnonymousType_02[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。
【问题讨论】:
-
为什么一定是DataTable?
-
StaffOfficeAccess 表没有办公室名称实体。我需要返回办公室 ID 和办公室名称。可以是数据集,因为我需要将结果与下拉列表控件绑定。