简体   繁体   中英

How to write LINQ query?

关注

状态更新

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM