实体框架Linq查询3个表

时间:2011-08-08 06:53:59

标签: entity-framework

我有这个3表查询,不会返回任何结果。有人对我做错了什么有任何建议吗? _id是一个全局变量。

'If there is a guest list then display each person in it
    Dim guests = (From g In myEntities.GuestLists
                  From p In myEntities.Pictures
                  From u In myEntities.UserProfiles
                  Where g.EventID = _id
                  Where p.UserID = g.GuestID
                  Where u.UserID = g.GuestID
                  Select New With {p.ImUrl, u.FirstName, u.LastName, u.UserID}).SingleOrDefault()
    Repeater1.DataSource = guests
    Repeater1.DataBind()

此语法适用于单个表或两个表查询,因此我假设3个表是相同的。

1 个答案:

答案 0 :(得分:0)

我认为你必须JOIN表。

喜欢这个(未经测试!)

From g In myEntities.GuestLists
Join p In myEntities.Pictures On g.GuestID Equals p.UserID
Join u In myEntities.UserProfiles On g.GuestID Equals u.UserID
Where g.EventID == _id
Select New With {p.ImUrl, u.FirstName, u.LastName, u.UserID}