2014-03-05 41 views
-1

我有linqpad下面的查詢工作,但它不工作時,我把它放在Visual Studio中,因爲aspnet_UsersInRoles顯示爲EDMX而不是一個關聯實體。我怎樣才能解決這個問題?如何使用LINQ協會,實體不可用

var memberships = from m in First5MembershipDB.aspnet_Membership 
        join u in First5MembershipDB.aspnet_Users on m.UserId equals u.UserId 
        join ur in First5MembershipDB.Aspnet_UsersInRoles on u.UserId equals ur.UserId 
        join r in First5MembershipDB.aspnet_Roles on ur.RoleId equals r.RoleId 
        join a in First5MembershipDB.Applications on r.WebApplicationID equals a.ApplicationId 
+0

使用關聯。 'u.Members' – SLaks

+0

你有個例子嗎? – user1202606

回答

0

當你在LINQ墊您使用在您的項目中定義的同一實體(EDMX)這樣做,或者您使用的是自動生成的模型(LINQ到SQL)?如果EF將Aspnet_UsersInRoles看作是Many-Many表,它可以優化它並要求您按如下方式訪問它:

var memberships = from m in First5MembershipDB.aspnet_Membership 
        join u in First5MembershipDB.aspnet_Users on m.UserId equals u.UserId 
        from r in u.aspnet_Roles 
        join a in First5MembershipDB.Applications on r.WebApplicationID equals a.ApplicationId 
+0

非常有幫助,謝謝。我以前沒有用過。 – user1202606