RahulD
RahulD

Reputation: 709

Updating a List, consiting of another List

I have a List named users of type User class. User class has properties Id, Name, Location, List<string>MyWall,List<User> FriendList. There are three tables

User(UserId(PK),Name,Location), 
UserPost(PostID(PK),UserID(FK),WallText), 
UserFriends (UFId (PK),FriendID(FK),UserID(FK))

I'm first creating a new user by asking the details of Name and Location. These details are then inserted into User table and after reading these details from the database I'm storing them into a List<User> users = new List<User>(), so that I could use this list further for displaying, instead of fetching the data from database.

Till here everything works fine. Now after creating the user, I'm asking the user to write the posts on his wall (can be multiple posts) and these details are inserted into UserPost table. Further I'm fetching the posts of a user from the database corresponding to the user ID, now I want these posts to get updated in the users list but when I add the wall post (users.Add(user)), entire user details gets added as a new user. Please tell me how can I update my users list with Wall Posts, corresponding to the existing user details.

My Code-

Here I'm adding user details without WallPost, to the users list-

private void DisplayAllUsers()
        {
            connection.Open();
            SqlCommand command = new SqlCommand("DisplayUsers", connection);
            using (SqlDataReader reader = command.ExecuteReader())
            {
                command.CommandType = CommandType.StoredProcedure;

                Console.WriteLine("Details of all the Users: ");
                while (reader.Read())
                {
                    User user = new User()
                    {
                        ID = int.Parse(reader["UserId"].ToString()),
                        Name = reader["Name"].ToString(),
                        Location = reader["Location"].ToString()
                    };
                    users.Add(user);
                    Console.WriteLine();
                    Console.WriteLine("ID: " + user.ID + " Name: " + user.Name + " Location: " + user.Location);
                }
            } 

Display Wall Method (Here I have to update the users list)-

private void DisplayWall(User user)
        {
            OnDisplayDetails(user);
            connection.Open();
            SqlCommand command = new SqlCommand("select WallText from UserPost where UserID='"+ user.ID +"'", connection);
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine(user.Name + " says " + reader["WallText"]);
                }
                int count = users.Count;
//This LINQ doesn't work because MyWall is of type List<string>
                //(from u in users
                // where u.ID == user.ID
                // select u).ToList().ForEach(u => u.MyWall = //reader["WallText"]);
                //Here While Adding the User Details to the users list, it gets added with //all the details 
                   users.Add(user);
            }
            connection.Close();
        }

Upvotes: 0

Views: 124

Answers (1)

Andrew Cooper
Andrew Cooper

Reputation: 32586

What about user.MyWall.Add(reader["WallText"]) inside the read loop? You probably want to check if the post is already in the list before you add it, though.

Upvotes: 3

Related Questions