2017-03-07 77 views
-1

这是我的代码3个表:如何加入使用LINQ语法

// Put together a list of new communities 
var communityList = from x in db.CommunityTeams 
        join y in db.Communities on x.CommunityId equals y.CommunityId 
        join z in db.CommunityRoles on x.CommunityRole equals z.Role 
        where x.UserId == userId 
        select new 
        { 
         CommunityName = y.ComunityName, 
         CommunityRoleName = z.Role  
        }; 

在db.CommunityRoles的加入z为给我这个错误:

在不正确的加入条款。我如何获得正确的语法?

+0

给你什么错误? –

+1

请显示您的模型结构和您收到的错误。 – CodingYoshi

+0

我得到的错误是“Join子句不正确” –

回答

1

您的语法不正确。您加入的列表x.CommunityRolez.Role不在同一类型中。

Error CS1941 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.

你可能得到这个错误,你必须加入列具有相同的类型,例如intint。检查他们两个必须是相同的。

+0

感谢您的帮助,事实证明我已经在实体框架中的模型中使用了CommunityRole数据,因为我创建了一个外键约束,所以当我加载它时,社区团队中有一个CommunityRoles集合,从模型中,因此不需要连接。 –