gpt4 book ai didi

c# - 如何编写 LINQ 查询?

转载 作者:行者123 更新时间:2023-11-30 17:14:33 25 4
gpt4 key购买 nike

FollowingUsers

StatusUpdates

在上方您可以看到 FollowingUsersStatusUpdates 表。

FollowingUsers 中,我存储了 FollowerUsernameFollowingUsername。在 StatusUpdates 中,我存储了用户的状态更新

您可以在下面看到我为检索登录用户的状态更新而编写的原始查询。

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

关于c# - 如何编写 LINQ 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com