【发布时间】: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;
}
这就是我想不通的地方
- 如何获取 FollowerIds(作为一个数组(我认为它们需要是一个数组,但我不确定))
- '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