我需要使用实体框架将列表加入数据库查询。基本上,我有两个数据库,我需要将一个数据库中的表外部连接到另一个数据库中的表。为了做到这一点,我想从第一个数据库中获取一个列表,然后将它加入到第二个查询的查询中。问题是,我必须将结果作为 IQueryable 返回,因此该方法的使用者可以在执行查询之前对查询进行额外的过滤。
我尝试了两种不同的方法:
选项1:
Public Shared Function List() As IQueryable(Of ContractEquipment)
' Get Subcontractors from Database 1
Dim subcontractors As List(Of SubContractor) = GetSubContractorList().ToList()
' Query Database 2
Return (From c In DB.ContractEquipments
Select New With {
.ContractID = c.ContractID,
.EquipmentID = c.EquipmentID,
.Inactive = c.Inactive,
.SubContractorName = (From x In subcontractors Where c.SubContractorID = x.SubContractorID Select x.Name).FirstOrDefault()
})
结束功能
选项 2:
Public Shared Function List() As IQueryable(Of ContractEquipment)
' Get Subcontractors from Database 1
Dim subcontractors As List(Of SubContractor) = GetSubContractorList().ToList()
' Query Database 2
Return (From c In DB.ContractEquipments
Group Join s in subcontractors on c.SubContractorID equals s.SubContractorID into tblSubContractors = Group
Select New With {
.ContractID = c.ContractID,
.EquipmentID = c.EquipmentID,
.Inactive = c.Inactive,
.SubContractorName = tblSubContractors.Select(Function(z) z.Name).FirstOrDefault()
})
结束功能
我不断收到相同的错误:“无法创建类型的常量值。在此上下文中仅支持原始类型('例如 Int32、String 和 Guid')。”
有人有什么建议吗?