我一直在为我的应用程序使用实体框架。不幸的是,我无法在实体框架中创建这样的表达式:
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; }
}
答案 0 :(得分:3)
假设MyRoles是实体模型中的关联属性(即映射到表),您可能希望在角色对象的主键上进行显式匹配。例如:
user.MyRoles.Any(r => r.RoleId == existingRole.RoleId)
但是,您的问题中没有足够的信息来提供确切的答案。