gpt4 book ai didi

c# - Entity Framework linq 查询中 IEnumerable.Contains() 的解决方案

转载 作者:行者123 更新时间:2023-11-30 22:21:38 30 4
gpt4 key购买 nike

我一直在为我的应用程序使用 Entity Framework 。不幸的是,我不能在 Entity Framework 中做出这样的表达:

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; }
}

最佳答案

假设 MyRoles 是实体模型中的关联属性(即映射到表),您可能希望在角色对象的主键上进行显式匹配。例如:

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

不过,您的问题中没有足够的信息来给出准确的答案。

关于c# - Entity Framework linq 查询中 IEnumerable.Contains() 的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14222626/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com