gpt4 book ai didi

c# - 论坛的 LINQ 查询

转载 作者:行者123 更新时间:2023-11-30 16:36:22 26 4
gpt4 key购买 nike

我正在为这个论坛编写代码,因为我是 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 查询,如果它需要优化请告诉我。

关于c# - 论坛的 LINQ 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/931617/

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