如何从linq" .include"获取其他表信息。

时间:2015-04-13 19:12:24

标签: database vb.net linq entity-framework

我试图通过使用这个来获取Table1和Table2中的信息....

Public Shared Function SelectDataByDateAndLastName(ByVal Date As Date, ByVal TxtBoxLastName As String)

    Dim TableItems As ObjectQuery(Of tblTable1) = From TableItem In tblTable1.Include("tblTable2")
                                                        Where TableItem.ItemDate = Date _
                                                        And tblTable2.LastName = TxtBoxLastName
    Return TableItems

但是我在tblTable2.LastName

上收到错误
Reference to a non-shared member requires an object reference

有关如何从Table2获取LastName信息的任何想法?或者我甚至没有使用这个权利?谢谢!

1 个答案:

答案 0 :(得分:1)

应该有一个导航属性 - 您可以像这样访问它:

Dim TableItems As ObjectQuery(Of tblTable1) = 
    From TableItem In tblTable1.Include("tblTable2")
    Where TableItem.tblTable2.LastName = TxtBoxLastName