Union和Option Strict的转换问题

时间:2011-05-20 09:44:30

标签: vb.net linq-to-sql union

我对LINION to SQL的UNION有问题

我有我的LINQ查询

Dim dc As New ContainerDB.DataClassesDataContext()
    Dim test = (From container In dc.GetTable(Of tbl_container)() Where container.pkContainerID = iPkContainerID).Union( _
                    From containerHist In dc.GetTable(Of tbl_containerHIST)() Where containerHist.pkContainerID = iPkContainerID)

我总是得到隐式转换错误

  

System.Linq.IQueryable(中   ContainerDB.tbl_containerHIST)in   System.Collections.Generic.IEnumerable(中   ContainerDB.tbl_container)。

该查询有什么问题? 我错过了什么?

提前致谢

1 个答案:

答案 0 :(得分:1)

尝试放置Option Infer On。这将允许使用类型推断,您似乎在这里做:

Dim test =

修改

此外,您需要使用anonymous types:,因此可以比较tbl_container和tbl_containerHIST:

Dim test = (From container In dc.GetTable(Of tbl_container)() 
            Where container.pkContainerID = iPkContainerID 
            Select New With{ ... }).Union( _
            From containerHist In dc.GetTable(Of tbl_containerHIST)() 
            Where containerHist.pkContainerID = iPkContainerID 
            Select New With{ ... })