Linq 返回ApplicationUser对象或ApplicationSerID字符串的列表

Linq 返回ApplicationUser对象或ApplicationSerID字符串的列表,linq,asp.net-core,Linq,Asp.net Core,在我的ASP.NET核心项目中,我有一个Twitter风格的followers/following设置 我正在尝试构建一个LINQ查询,它将返回属于我和我“关注”的用户网络的记录。大概是这样的: .Where(o => usersFollowingList.Contains(o.ApplicationUser.Id)) var g = from p in loggedinUser.Following select p.ApplicationUser.Id.ToString(); 我的追

在我的ASP.NET核心项目中,我有一个Twitter风格的followers/following设置

我正在尝试构建一个LINQ查询,它将返回属于我和我“关注”的用户网络的记录。大概是这样的:

.Where(o => usersFollowingList.Contains(o.ApplicationUser.Id))
var g = from p in loggedinUser.Following
select p.ApplicationUser.Id.ToString();
我的追随者/以下设置是与.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; }
}
但它不包含我自己的ApplicationSerid。我无法轻松地将自己的ApplicationSerID添加到此集合,因为它是IEnumerable类型


如何获得可以在WHERE子句中使用的ApplicationUser对象或ApplicationSerID字符串的适当集合?或者有更好的方法使用我的WHERE过滤器中的Followers列表吗?

您可以使用
Concat
添加两个
IEnumerable
,因此您只需将自己转换为singleton
IEnumerable
。我更喜欢一种扩展方法:

public static IEnumerable<T> Append<T>(this IEnumerable<T> rest, params T[] last) => rest.Concat(last);
但是,如果您已经拥有以下
网络
对象,为什么要在
的Where
中使用
g

var g = loggedinUser.Following
                    .Append(loggedinUser);
当然,您也可以执行
Where
,但这是不必要的搜索:

.Where(o => usersFollowingList.Contains(o.ApplicationUser.Id) || o.ApplicationUser.Id == loggedinUser.Id)

谢谢@NetMage。我将标记为“已回答”。我只是想继续你的话:“但是如果你已经有了下面的网络对象,为什么还要在Where中使用g呢?”。如何使用以下网络对象?WHERE需要ApplicationUser或Id。我似乎无法获取查询以使用来自网络对象的信息。有什么想法吗?:)'以下“”属于“网络”类型。但我需要在查询中键入ApplicationUser;当
后面的
已经包含您的
Where
似乎要返回的网络对象时,为什么要使用
Where
.Where(o => usersFollowingList.Contains(o.ApplicationUser.Id) || o.ApplicationUser.Id == loggedinUser.Id)