【发布时间】:2018-03-06 07:59:17
【问题描述】:
我的 ASP.NET Core 项目中有一个 Twitter 风格的追随者/追随者设置。
我正在尝试构建一个 LINQ 查询,该查询将返回属于我和我“关注”的用户网络的记录。像这样的:
.Where(o => usersFollowingList.Contains(o.ApplicationUser.Id))
我的关注者/关注设置是与 .NET Core 的 IdentityUser 的自引用关系:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Network> Following { get; set; }
public virtual ICollection<Network> Followers { get; set; }
}
public class Network
{
public ApplicationUser ApplicationUser { get; set; }
public string ApplicationUserId { get; set; }
public ApplicationUser Follower { get; set; }
public string FollowerId { get; set; }
}
此设置为我提供了我关注的用户列表。该集合具有 ApplicationUser 对象及其字符串类型的 ApplicationUserId。
我在尝试获取可以在上面的 WHERE 子句中使用的 ApplicationUser 对象或 ApplicationUserId 字符串的集合时遇到问题。
我可以像这样获取我的关注者的 ApplicationUserId 字符串列表:
var g = from p in loggedinUser.Following
select p.ApplicationUser.Id.ToString();
但这不包含我自己的 ApplicationUserId。而且我无法轻松地将自己的 ApplicationUserId 添加到此集合中,因为它是 IEnumerable 类型。
如何获得可以在 WHERE 子句中使用的 ApplicationUser 对象或 ApplicationUserId 字符串的适当集合?或者有没有更好的方法在我的 WHERE 过滤器中使用关注者列表?
【问题讨论】:
标签: linq asp.net-core