Я получил все топики, которые относятся к указанному форуму следующим путём:
public async Task<IActionResult> Index(int forumId)
{
Forum forum = await _dbContext.Forum.SingleOrDefaultAsync(f => f.Id == forumId);
if (forum == null)
return RedirectToAction("AccessDenied", "Error");
IEnumerable<Topic> topics = await _dbContext.Topic.Include("Owner")
.Where(t => t.ForumId == forumId && t.Id == t.RootTopicId)
.OrderByDescending(t => t.PostDateTime).ToListAsync();
ViewBag.ForumId = forum.Id;
ViewBag.ForumName = _dbContext.Forum.SingleOrDefault(f => f.Id == forumId).Name;
return View(topics);
}
Теперь хочу получить последних отписавшихся. Думал как-то таким путём, но получаю не последних отписавшихся, а авторов...
var last = await _dbContext.Topic
.Where(t => t.ForumId == forumId && t.Id == t.RootTopicId)
.OrderBy(t => t.PostDateTime != DateTime.MaxValue).FirstOrDefaultAsync();
Класс Topic:
public partial class Topic
{
public Topic()
{
InverseReplyToTopic = new HashSet<Topic>();
InverseRootTopic = new HashSet<Topic>();
}
public int Id { get; set; }
public int? OwnerId { get; set; }
public int ForumId { get; set; }
public int? RootTopicId { get; set; }
public int? ReplyToTopicId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PostDateTime { get; set; }
public int? ModifiedByUserId { get; set; }
public DateTime? ModifyDateTime { get; set; }
public bool IsLocked { get; set; }
public Forum Forum { get; set; }
public User ModifiedByUser { get; set; }
public User Owner { get; set; }
public Topic ReplyToTopic { get; set; }
public Topic RootTopic { get; set; }
public ICollection<Topic> InverseReplyToTopic { get; set; }
public ICollection<Topic> InverseRootTopic { get; set; }
}
Класс User:
public partial class User
{
public User()
{
Forum = new HashSet<Forum>();
MessageFromUser = new HashSet<Message>();
MessageToUser = new HashSet<Message>();
TopicModifiedByUser = new HashSet<Topic>();
TopicOwner = new HashSet<Topic>();
}
public int Id { get; set; }
public string Login { get; set; }
public string Name { get; set; }
public string PasswordHash { get; set; }
public bool IsAdministrator { get; set; }
public bool IsLocked { get; set; }
public DateTime RegisterDateTime { get; set; }
public DateTime LastLoginDateTime { get; set; }
public ICollection<Forum> Forum { get; set; }
public ICollection<Message> MessageFromUser { get; set; }
public ICollection<Message> MessageToUser { get; set; }
public ICollection<Topic> TopicModifiedByUser { get; set; }
public ICollection<Topic> TopicOwner { get; set; }
}
Класс Forum:
public partial class Forum
{
public Forum()
{
Topic = new HashSet<Topic>();
}
public int Id { get; set; }
public int CategoryId { get; set; }
public int OwnerId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime CreateDateTime { get; set; }
public bool IsLocked { get; set; }
public User Owner { get; set; }
public ICollection<Topic> Topic { get; set; }
public Category Category { get; set; }
}
Скриншот таблицы с тестовыми записями:
Дополню: Тут хочу получить того, кто оставил последнее сообщение и дату:
[AllowAnonymous]
public async Task<IActionResult> Index()
{
IEnumerable<Category> categories = await _dbContext.Category
.Include(c => c.Forum)
.OrderBy((Category cat) => cat.Id)
.ToListAsync();
foreach (var cat in categories)
cat.Forum = cat.Forum.OrderBy(forum => forum.Id).ToList();
var last = await _dbContext.Topic.Where(tx => tx.RootTopicId == 1).OrderByDescending(tx => tx.PostDateTime == DateTime.MaxValue).ToListAsync();
ViewBag.Last = last.FirstOrDefault().OwnerId;
return View(categories);
}
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости