繁体   English   中英

如何编写LINQ查询?

[英]How to write LINQ query?

关注

状态更新

上面你可以看到FollowingUsersStatusUpdates表。

FollowingUsers ,我存储了FollowerUsernameFollowingUsername StatusUpdates ,我存储用户的Status updates

下面你可以看到我写的原始查询来检索登录用户的状态更新。

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

如何从登录用户的以下状态获取状态更新?

以下应该可以工作,虽然我没有你的数据库来测试它。 请注意,在调用ToList之前实际上不会执行它,因此所有内容仍应在单个数据库查询中发生。 此外,不需要创建新列表,因为它将被您的查询覆盖,所以我已经整理了一点。

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