2017-03-07 52 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集合,從模型中,因此不需要連接。 –