0

I have two tables. Post table and Follow table. Post table has all the posts of a user and Follow table has the list of user followed by a user.

Post Table

PostID    UserID      Post
1            2       TextOne
2            1       TextTwo
3            1       Text3
4            2       Text4

Follow Table

ID     Following        FollowedBy
1        2                  1
2        3                  1

And I have a list view.

<ItemTemplate >       
    Post:
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Post") %>' />
    <br />
    UserID:
    <asp:Label ID="Label2" runat="server" Text='<%# Eval("UserID") %>' />
    <br />          
 </ItemTemplate>

I want to show the posts of the user and the people who he are following. I wrote the following code.

int UserId = Convert.ToInt32(Session["User"]);
int[] OthersPosts = (from s in Data.Follow where s.FollowedBy == UserId) select s.Following1).ToArray();

foreach (int post in OthersPosts)
{
    var DisplayPost = (from s in Data.Posts where s.UserID == post && s.UserID == UserId) select s).ToList();
    ListViewPostTable.DataSourceID = "";
    ListViewPostTable.DataSource = DisplayPost;
    ListViewPostTable.DataBind();
}

But no data is displayed on the ListView?

I have checked the watch window for the value in DisplayPost variable, it says Enumeration yielded no results.

4
  • Whats the return value in OthersPosts array ? Commented Jan 7, 2014 at 5:26
  • The user id's of the persons followed by 'UserId' Commented Jan 7, 2014 at 5:32
  • i thinks you have correct the query may be like this: var DisplayPost = (from s in Data.Posts where s.PostID == post && s.UserID == UserId) select s).ToList(); Commented Jan 7, 2014 at 5:37
  • but the OtherPosts array is array of UserId ,how can i search in PostID ? i just named it post but it is user Commented Jan 7, 2014 at 5:42

2 Answers 2

2

The error is in the following predicate:

s.UserID == post && s.UserID == UserId

Since UserID is always different from post which is one of the users he is following, this predicate always returns false.

You should at least change it to

s.UserID == post || s.UserID == UserId

I.e. you want to find posts by the user himself or one of the users he's following.

This still won't work as expected if he's following more than one user because in the foreach loop you'll be binding data to ListViewPostTable multiple times. Of course at the end you'll only see the last result.

Try replacing the foreach loop like this:

var DisplayPost = (
    from s in Data.Posts 
    where s.UserID == UserId || OthersPosts.Contains(s.UserID)
    select s).ToList();
ListViewPostTable.DataSourceID = "";
ListViewPostTable.DataSource = DisplayPost;
ListViewPostTable.DataBind();
Sign up to request clarification or add additional context in comments.

3 Comments

+ 1 for using Contains, instead of foreach.
@GeneliaD'Souza you would want to remove the s.UserID == UserId || from the where clause as it would load the Session["User"]'s posts as well. Which not what you want, I guess.
@DevrajGadhavi i want that too
0

Change your code like this.

int UserId = Convert.ToInt32(Session["User"]);
int[] OthersPosts = (from s in Data.Follow where s.FollowedBy == UserId) select s.Following1).ToArray();

List<Posts> lstPosts = new List<Posts>();

foreach (int post in OthersPosts)
{
    var displayPost = (from s in Data.Posts where s.UserID == post) select s).ToList();
    lstPosts.AddRange(displayPost);
}

ListViewPostTable.DataSourceID = "";
ListViewPostTable.DataSource = lstPosts;
ListViewPostTable.DataBind();

You were matching the s.UserID == post && s.UserID == UserId. Which is wrong, because a post will not belong to two users.

5 Comments

Following and FollowedBy are the user Id's of the user in follow table, in the query "int[] OthersPosts = (from s in Data.Follow where s.FollowedBy == UserId) select s.Following1).ToArray();" how can i load Person that iam following into the array 'OthersPost' and search for 'PostId' ? and still it is not working i think it is wrong
OthersPost is loading UserId's
Yes, it would load UserIDs. And in the foreach loop you would get their posts.
but how to display posts of all users that a user follows ?
See the edited answer. The foreach loop would fetch of all the posts from users who are being followed by the Session["User"]. And those posts would be added to the List variable lstPosts. Finally after getting all posts from all users, lstPosts would be assigned to ListViewPostTable.DataSource.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.