论坛的 LINQ 查询

2023-12-22

我正在为这个论坛编写代码,由于我是 LINQ 新手,所以当用户点击主页时遇到了这个问题。我想要一个显示论坛列表的表格,如下所示:

Forum  --- Topics (count) --- Posts (count) --- LastPostUserId --- LastPostTime

我有以下 SQL 表:

Forums:
ForumId (int32),
Title (string),
Description (string)

ForumThreads:
ThreadId (int32),
ForumId (int32),
UserId (guid),
Subject (string),
Views (int32),
CreateDate (DateTime)

ForumPosts:
PostId (int32),
ThreadId (int32),
UserId (guid),
Post (string),
CreateDate (datetime)

谢谢...


from forum in forums
from posts in db.ForumPosts.Where(p => p.Thread.ForumId.Equals(forum.ForumId))
select new
{
Forum = forum.Title, 
Topics = forum.ForumThreads.Count(),
Posts = posts.Count(),
LastPostBy = posts.OrderByDescending(p => p.CreateDate).FirstOrDefault(p => p.UserId),
LastPostTime= posts.Max(p => p.CreateDate))
}

当然未经测试,但尝试从这里开始并检查它执行的 SQL 查询,并让我知道它是否需要优化。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

论坛的 LINQ 查询 的相关文章

随机推荐