简体   繁体   English

如何编写LINQ查询?

[英]How to write LINQ query?

关注

状态更新

Above you can see FollowingUsers and StatusUpdates tables. 上面你可以看到FollowingUsersStatusUpdates表。

In FollowingUsers , I store Follower 's Username and Following 's Username . FollowingUsers ,我存储了FollowerUsernameFollowingUsername In StatusUpdates , I store Status updates of users. StatusUpdates ,我存储用户的Status updates

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. 请注意,在调用ToList之前实际上不会执行它,因此所有内容仍应在单个数据库查询中发生。 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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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