Вопрос:

find all posts of user friend list by linq

c# vb.net linq

61 просмотра

3 ответа

8 Репутация автора

I have 2 tables ( tblPosts , tblFriends ) like bellow tables :

tblPost:
--------
PostID         ( nvarchar )
PostValue      ( nvarchar )
PostSender     ( nvarchar )

' '

tblFriends:
------------
ID             ( int )
User1          ( nvarchar )
User2          ( nvarchar )

now my question is , how can I find all posts of user1 friends by LINQ ?

Автор: Ali Источник Размещён: 08.11.2017 11:03

Ответы (3)


0 плюса

16764 Репутация автора

First, find the friends of user1:

var friends = from f in tblFriends
              where f.User1 == user1
              select f.User2;

Then find all the posts sent by those friends:

var friendsPosts = from p in tblPost
                   where friends.Contains(p.PostSender)
                   select p;
Автор: NetMage Размещён: 08.11.2017 11:17

0 плюса

8 Репутация автора

    Dim q = From fr In db.tblFriends, post In db.tblPosts
                Where fr.User1 = Username And post.Username = fr.User2
                Order By post.RegDate Descending
                Select post
Автор: Ali Размещён: 09.11.2017 12:16

0 плюса

18 Репутация автора

Your question is not completed. So write clearly with a relationship of the table and proper format of the output. If you want all posts of belonging user1 then try the following query.

     var val = (from a in tblPost
                 join b in tblFriends on a.PostSender equals b.User1
                 select a).ToList();
Автор: Mehul Prajapati Размещён: 09.11.2017 04:41
32x32