ChamingaD
ChamingaD

Reputation: 2928

How to write LINQ query?

FollowingUsers

StatusUpdates

Above you can see FollowingUsers and StatusUpdates tables.

In FollowingUsers, I store Follower's Username and Following's Username. In StatusUpdates, I store Status updates of users.

Below you can see original query I wrote to retrieve status updates of user who logged in.

var list = new List<StatusUpdate>();
list = (from x in db.StatusUpdates 
        where x.Author == User.Identity.Name 
        orderby x.Timestamp descending 
        select x)
       .Take(count).ToList(); 

How to get status updates from followings of logged in user?

Upvotes: 3

Views: 2123

Answers (1)

ForbesLindesay
ForbesLindesay

Reputation: 10712

The following should work, although I don't have your database to test it on. Note that it won't actually be executed until the call to ToList so everything should still happen in a single database query. Also, the creation of a new list is not needed as it will be overwritten by your query so I've tidied that up a little.

var Users = from f in db.FollowingUsers 
            where f.FollowerId == User.Identity.Name
            select f.FollowingId;

var list = (from x in db.StatusUpdates 
           from y in Users
           where x.Author == y 
           orderby x.Timestamp descending 
           select x)
           .Take(count).ToList(); 

Upvotes: 2

Related Questions