Linq查询来自IEnumerable的整数

时间:2014-12-05 18:37:20

标签: c# asp.net-mvc linq

我在ASP.NET MVC项目中遇到了逻辑问题。我正在开发一个看起来像Facebook的社交媒体应用。除了一件事,我得到了一切。

该应用程序有一个名为Follow的表,其中包含loggedInUser_IDuserToFollow_ID。 我有一个IEnumerable个整数,其中包含登录用户的usersToFollow_ID列表,方法如下:

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;
}

如何预测这些整数以获取loggedInUser想要​​关注的所有帖子?

我可以使用Linq查询解决此问题吗?

2 个答案:

答案 0 :(得分:0)

这样的事情会起作用吗?这假设UserPosts有一对多的关系:

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);
}

修改

为了测试这一点,由于我没有你的数据库,我只是制作了一些虚拟课来嘲笑你的。如果这些与你的相似,那么这应该有效。

User表,其中包含IdPost

列表
public class User
{
    public int Id { get; set; }
    public List<Post> Posts { get; set; }
}

Post表,其中只有Text属性(用于测试)

public class Post
{
    public string Text { get; set; }
}

Follow表,其中包含loggedInUser和userToFollow

public class Follow
{
    public User loggedInUser { get; set; }
    public User userToFollow { get; set; }
}

&#39; db&#39;,其中包含User列表和Follow列表(这些是表格)

public static class db
{
    public static List<Follow> Follow = new List<Follow>();
    public static List<User> Users = new List<User>();
}

然后我创建了一个用10个用户填充数据库的方法。用户ID&#39; 0&#39;是&#34;已登录的用户&#34;,用户1 - 9后面跟着用户0,每个帖子都有4个帖子

/// <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)
}

答案 1 :(得分:-1)

只需添加对ToList()的调用:

public IEnumerable<Post> postsForFlow(int userID, string userName)
{
    var userToFollowID = db.Follow.Where(a => 
                  a.loggedInUser.Id == userID).Select(b => b.userToFollow.Id).ToList();
    userToFollowID.ForEach(user =>
          {
                Follow(user); // Use whatever logic you need for following.  "user" is the int you want
          });
    return null;
}