C# 从整数的IEnumerable进行Linq查询
我在C# 从整数的IEnumerable进行Linq查询,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我在ASP.NET MVC项目中遇到了一个逻辑问题。我正在开发一个类似Facebook的社交媒体应用程序。除了一件事,我什么都要做 该应用程序有一个名为Follow的表,其中包含loggedInUser\u ID和userToFollow\u ID。 我有一个IEnumerable整数,其中包含users的列表。对于登录用户,该方法如下所示: public IEnumerable<Post> postsForFlow(int userID, string userName) {
ASP.NET MVC
项目中遇到了一个逻辑问题。我正在开发一个类似Facebook的社交媒体应用程序。除了一件事,我什么都要做
该应用程序有一个名为Follow
的表,其中包含loggedInUser\u ID
和userToFollow\u ID
。
我有一个IEnumerable
整数,其中包含users的列表。对于登录用户,该方法如下所示:
public IEnumerable<Post> postsForFlow(int userID, string userName)
{
IEnumerable<int> userToFollowID = db.Follow.Where(a =>
a.loggedInUser.Id == userID).Select(b => b.userToFollow.Id);
return null;
}
public IEnumerable postsForFlow(int userID,string userName)
{
IEnumerable userToFollowID=db.Follow.Where(a=>
a、 loggedInUser.Id==userID);
返回null;
}
如何对这些整数进行foreach,以获得loggedInUser希望遵循的所有帖子
我可以用Linq查询解决这个问题吗?只需添加对ToList()的调用:
public IEnumerable postsForFlow(int userID,string userName)
{
var userToFollowID=db.Follow.Where(a=>
a、 loggedInUser.Id==userID);
userToFollowID.ForEach(用户=>
{
Follow(user);//使用您需要的任何逻辑进行跟踪。“user”是您想要的int
});
返回null;
}
您喜欢这项工作吗?这是假设用户
与帖子
有一对多关系:
public IEnumerable<Post> PostsForFlow(int userId)
{
var userIdsToFollow = db.Follow.Where(a =>
a.loggedInUser.Id == userId).Select(b => b.userToFollow.Id);
return db.Users
.Where(u => userIdsToFollow.Contains(u.Id))
.SelectMany(u => u.Posts);
}
Post
表,它只有一个Text
属性(用于测试)
Follow
表,其中包含loggedInUser和userToFollow
public class Follow
{
public User loggedInUser { get; set; }
public User userToFollow { get; set; }
}
“db”,其中包含用户
列表和后续
列表(这些是表格)
然后,为了测试它,我只调用两种方法:
public static void Main()
{
PopulateDb();
var postsForFlow = PostsForFlow(0);
// Now postsForFlow has 36 entries - 4 for each
// of the 9 users that user '0' is following
Console.WriteLine(postsForFlow.Count());
// Output: 36 (9 users * 4 posts each)
// Now, to test it further, stop following all users who have even Ids
// This will leave us with 5 users: 1, 3, 5, 7, 9
foreach (var follow in db.Follow
.Where(f => f.userToFollow.Id % 2 == 0)
.ToList())
{
db.Follow.Remove(follow);
}
postsForFlow = PostsForFlow(0);
Console.WriteLine(postsForFlow.Count());
// Output: 20 (5 users * 4 posts each)
}
您不应该在内存中执行此操作,而应该在实际的数据库查询中执行此操作。您的模型是否具有导航属性?我怀疑您的Select
子句应该使用一个或多个这些属性来返回Post
对象的集合。只是猜测一下这里的结构,但是类似于:.SelectMany(b=>b.userToFollow.Posts)
我们不知道您打算如何将一组用户ID转换为后续的帖子。你想怎么做呢?你想显示所有的帖子,最近的帖子,随机选择的最近的帖子,其他人感兴趣的帖子,他们还没有看到的帖子,还是什么?我们可以(潜在地)帮助您找出如何实现您想要的内容,但我们不知道您想要实现什么。@Servy我想展示loggedInUser正在关注的所有帖子。我在IEnumerable中有所有这些用户。@wallef是的,你有他们关注的所有用户,但是你想如何确定他们关注的所有帖子?您是否将他们关注的帖子定义为他们关注的所有用户的所有帖子?他们是否与用户分开关注帖子?你想确定他们正在关注的用户的一些帖子是“他们正在关注的帖子”吗?如果是,如何确定他们是哪些帖子?如果他确实想对数据进行foreach
,他只需使用foreach
,就可以了。当然,这实际上并没有让他得到他想要的数据(至少不是唯一的)。这里也没有理由将整个集合具体化为一个列表。最后,这项工作首先不应该在记忆中完成;这个查询应该在DB.Hmm上完成,当我尝试这个时,我只得到loggedInUser跟随的第一个用户的帖子。我想遍历loggedInUser所关注的所有用户帖子。感谢您的输入,目前为5。但是它可能更多,也可能很少,这取决于loggedinuser follow/unfollow userWell,我不知道您的确切模式,但它对我有效。userIdsToFollow
中的所有ID是否都存在于db.Users
中,并且它们是否都至少有一个Post
?您完全正确!它起作用了。但现在奇怪的是,我只能让property.Text工作,否则它会出错。在我看来:@foreach(Model.postsForFlow中的var项){@item.Text}
有效。但是如果我想像`@foreach(Model.postsForFlow中的var item){@item.Text说:@item.user}这样显示用户,我会得到一个“System.Data.Entity.Core.EntityCommandExecutionException”。有什么想法吗?好吧,您指定了一个返回帖子列表而不是用户列表的方法。只需更改这部分逻辑和返回值!
public class Post
{
public string Text { get; set; }
}
public class Follow
{
public User loggedInUser { get; set; }
public User userToFollow { get; set; }
}
public static class db
{
public static List<Follow> Follow = new List<Follow>();
public static List<User> Users = new List<User>();
}
/// <summary>
/// This populates the Db with 10 users. All users
/// from 1 to 9 are being followed by user 0
/// </summary>
public static void PopulateDb()
{
// Create the logged in user
var loggedInUser = new User { Id = 0, Posts = new List<Post>() };
db.Users.Add(loggedInUser);
// Create 9 other users with 4 posts each
for (int i = 1; i < 10; i++)
{
var newUser = new User
{
Id = i,
Posts =
new List<Post>
{
new Post {Text = "Post #" + i},
new Post {Text = "Post #" + (i * 10)},
new Post {Text = "Post #" + (i * 20)},
new Post {Text = "Post #" + (i * 30)}
}
};
// Add the other user
db.Users.Add(newUser);
// Have the logged in user follow this new user
db.Follow.Add(new Follow { loggedInUser = loggedInUser,
userToFollow = newUser });
}
}
public static void Main()
{
PopulateDb();
var postsForFlow = PostsForFlow(0);
// Now postsForFlow has 36 entries - 4 for each
// of the 9 users that user '0' is following
Console.WriteLine(postsForFlow.Count());
// Output: 36 (9 users * 4 posts each)
// Now, to test it further, stop following all users who have even Ids
// This will leave us with 5 users: 1, 3, 5, 7, 9
foreach (var follow in db.Follow
.Where(f => f.userToFollow.Id % 2 == 0)
.ToList())
{
db.Follow.Remove(follow);
}
postsForFlow = PostsForFlow(0);
Console.WriteLine(postsForFlow.Count());
// Output: 20 (5 users * 4 posts each)
}