2013-01-08 110 views
2

我一直在使用我的應用程序的實體框架。不幸的是,我不能在實體框架中製作像這樣的表達式:IEnumerable.Contains()在實體框架LINQ查詢中的解決方案

List<MyUser> users = (from user in database.MyUsers 
         join app in database.MyApplications 
          on user.ApplicationId equals app.Id 
         where app.Name == this._applicationName 
          && user.MyRoles.Contains(existingRole) 
         select user).ToList(); 

其中任何其他方法。我不想更改我的數據庫或我的模型。在我的情況下,MyUser和MyRole之間的關係是多對多的,有一個膠水錶。

這是MYUSER類是如何聲明:

public partial class MyUser 
{ 
    public MyUser() 
    { 
     this.MyApiTokens = new HashSet<MyApiToken>(); 
     this.MyLandingPages = new HashSet<MyLandingPage>(); 
     this.MyPresentations = new HashSet<MyPresentation>(); 
     this.MySlides = new HashSet<MySlide>(); 
     this.MyUserSettings = new HashSet<MyUserSetting>(); 
     this.MyRoles = new HashSet<MyRole>(); 
    } 

    public System.Guid Id { get; set; } 
    public string UserName { get; set; } 
    public string Password { get; set; } 
    public string EmailAddress { get; set; } 
    public int ApplicationId { get; set; } 
    public System.DateTime CreateDate { get; set; } 
    public System.DateTime LastActivityDate { get; set; } 
    public System.DateTime LastLockoutDate { get; set; } 
    public System.DateTime LastLoginDate { get; set; } 
    public System.DateTime LastPasswordChangedDate { get; set; } 
    public string PasswordQuestion { get; set; } 
    public string PasswordAnswer { get; set; } 
    public string Comment { get; set; } 
    public bool IsLocked { get; set; } 
    public bool IsApproved { get; set; } 

    public virtual ICollection<MyApiToken> MyApiTokens { get; set; } 
    public virtual MyApplication MyApplication { get; set; } 
    public virtual ICollection<MyLandingPage> MyLandingPages { get; set; } 
    public virtual ICollection<MyPresentation> MyPresentations { get; set; } 
    public virtual ICollection<MySlide> MySlides { get; set; } 
    public virtual ICollection<MyUserSetting> MyUserSettings { get; set; } 
    public virtual ICollection<MyRole> MyRoles { get; set; } 
} 
+2

可以在實體框架中使用'Queryable.Contains'。什麼錯誤信息給你? 'user.MyRoles'的類型是什麼? –

+0

在代碼它是一個ICollection Alecu

回答

3

假設MyRoles是在實體模型的關聯屬性(即映射到表),你可能想使主鍵匹配明確的角色對象。例如:

user.MyRoles.Any(r => r.RoleId == existingRole.RoleId) 

雖然您的問題中沒有足夠的信息來給出確切的答案。

+0

這是屬性的定義:public virtual ICollection MyRoles {get;組; }在MyUser類 – Alecu

+0

謝謝 - 你可以移動它到你的問題和格式作爲代碼,以便於閱讀? – sinelaw

+0

我在原文中增加了整個班級的定義 – Alecu