根据外键查找 table

Finding a table based on foreign key

我有这两个表和它们之间的桥梁:

public class User
    {
        [Required]
        public string Email { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public int Id { get; set; }
        public ICollection<UserLocation> UserLocations { get; set; }
        [Required]
        public string Password { get; set; }
        [Required]
        public string Username { get; set; }
    }
public class Location
    {
        [Required]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public string Picture { get; set; }
        public string PostCode { get; set; }
        public string Region { get; set; }
        public ICollection<UserLocation> UserLocations { get; set; }
    }
public class UserLocation
    {
        public Location Location { get; set; }
        public int LocationId { get; set; }
        public User User { get; set; }
        public int UserId { get; set; }
    }

我需要创建一个方法,将搜索字段和用户 ID 作为参数,returns 与该用户 ID 关联的所有位置,并搜索该字符串是否包含在任何位置的属性中.

我在根据用户 ID 返回位置时遇到问题。

我试过了_context.UserLocations.Include(ul=>ul.Location).Where(l=>l.UserId==userId) 但这没有用,因为我在尝试使用 l.UserId 时遇到语法错误。 我也尝试了相反的方法,_context.Locations.Include(l=>l.UserLocations) 但 运行 遇到了同样的问题。

我需要找到一种方法来检索与用户相关的所有位置。之后可以使用 Contains() 方法轻松完成搜索。

试试这个

_context.UserLocations.Where(x => x.UserId == userId).Select(x => x.Location)