【问题标题】:Query database for items using different Ids for the same object使用同一对象的不同 ID 查询数据库中的项目
【发布时间】:2020-04-06 17:20:38
【问题描述】:

所以我试图让所有的 'UserPhotos' 他们的 'userId' == 'FollowerId',当有多个 'FollowerId' 时我会遇到问题,我需要知道如何让它工作'followerIds' 列表,以便用户可以取回他们关注的所有“用户”的“用户照片”

    [HttpGet("u/{username}")]
    public async Task<IQueryable> GetSpecificFeed(string username)
    {
        var user = _context.Users.FirstOrDefault(x => x.Username == username);
        // Follower is the user that is being followed
        // Following is the user that is doing the following (so the current logged in user)

        var userId = user.Id;
        // This bit works and returns the correct userId

        var followerIds = _context.Follows.Where(x => x.FollowerId == userId);
        // This should return all of the 'FollowerIds' that the current user is following
        // This however doesn't do that, it just throws this error:
        // SQLite Error 1: 'no such column: x.UserId' (I don't know why)

        var feed = _context.UserPhotos.Where(x => x.UserId == userId);
        // This currently returns all of the 'UserPhotos' if their 'UserId' is equal to the 'UserId' 
        // (which is the id of the user that is logged in) -> This isn't what I want it to do
        // Once the 'followerIds' return the correct thing, it should then get the userPhotos where
        // The UserId == followerIds (followerIds will be an array, so it will have to check through
        // multiple values to get the whole 'feed'

        return feed;
    }

这就是我想不通的地方

  1. 如何获取 FollowerIds(作为一个数组(我认为它们需要是一个数组,但我不确定))
  2. 'userId' 有多个值时如何获取 UserPhotos

(p.s.我不确定标题好不好,如果我应该改变它,请告诉我)

[编辑]

这是下面的类

 public class Follow
    {
        public int Id { get; set; }

        public int FollowingId { get; set; }
        // user that is following

        public int FollowerId { get; set; }
        // user that is being followed
    }

用户类(我删除了与此示例无关的属性)

public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public ICollection<UserPhoto> UserPhotos { get; set; }
        public ICollection<Follow> Follows { get; set; }
    }

userPhotos 类(我删除了与此示例无关的属性)

public class UserPhoto
{
    public int Id { get; set; }
    public string photoUrl { get; set; }
    public int UserId { get; set; }
}

这是显示它应该如何工作的代码,但它没有(这显示了我想要完成的工作)

    [HttpGet("u/{username}")]
    public async Task<IQueryable> GetSpecificFeed(string username)
    {
        var user = _context.Users.FirstOrDefault(x => x.Username == username);
        // Follower is the user that is being followed
        // Following is the user that is doing the following (so the current logged in user)

        var userId = user.Id;

        var photos = _context.UserPhotos;

        var followerIds = _context.Follows.Where(x => x.FollowerId == userId);
        // Get all of the userIds of the users that the currentUser is 
        // following and put them into 'followerIds'

        var feeds = photos.Where(x => x.UserId == followerIds);
        // Search 'photos' for where the 'userId (the id of the user who 
        //created it)' matches the 'followersIds'

        return feeds ;
    }

【问题讨论】:

  • “没有这样的列”的异常与模型声明有关。尝试调查 The Follow 类并确定其关系。
  • 我已经这样做了,我已经添加了它们之间的关系,但看不到我做错了什么,我已经编辑了问题

标签: c# asp.net .net asp.net-core


【解决方案1】:

可以参考以下代码:

    [HttpGet("u/{username}")]
    public async Task<IQueryable> GetSpecificFeed(string username)
    {
        var user = _context.Users.FirstOrDefault(x => x.Username == username);
        var userId = user.Id;
        var photos = _context.UserPhotos;
        var followerIds = _context.Follows.Where(x => x.FollowerId == userId).Select(x => x.FollowingId).ToList();
        var feeds = _context.UserPhotos.Where(x => followerIds.Contains(x.UserId));
        return feeds;
    }

【讨论】:

  • 非常感谢,不过我有一个问题。你把'包含',没有;这意味着它可能会返回错误的结果,例如如果followersIds包含'1',它会不会因为followerId包含'1'而混淆并返回错误的结果,所以它也可以返回followerId为'10'(或其中包含1的任何数字)的结果
  • @chumberjosh,你完全不用担心。 followerIds 返回由不同 id 组成的a list int collection 。这里的contains是判断当前x.UserId是否等于followerIds数据集合之一。作为int数据来判断是否相等。而不是确定一个字符是否包含在字符串中。
【解决方案2】:

您应该首先触发 sql 查询。在您的情况下,您可以为此目的使用 ToList()。

[HttpGet("u/{username}")]
public async Task<IQueryable> GetSpecificFeed(string username)
{
    var user = _context.Users.FirstOrDefault(x => x.Username == username);

    var userId = user.Id;

    var followerIds = _context.Follows.Where(x => x.FollowerId == userId).ToList();

    var feeds = _context.UserPhotos.Where(x => followerIds.Contains( x.UserId ));

    return feeds;
 }

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2012-05-09
    • 2011-10-12
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多