public PartialViewResult RecentUnreadMessages(int count) { string userId = null; if (User != null) { userId = User.Identity.GetUserId(); } using (var db = new ZapContext()) { var user = db.Users //.Include("Alerts") .Include("Messages") //.Include("Alerts.PostLink") .Include("Messages.PostLink") .Include("Messages.From") .Where(u => u.AppId == userId).FirstOrDefault(); var vm = new RecentUnreadMessagesViewModel(); if (user != null) { var messages = user.Messages.Where(m => !m.IsRead && !m.IsDeleted).OrderByDescending(m => m.TimeStamp).Take(count); vm.Messages = messages.ToList(); } return(PartialView("_PartialRecentUnreadMessages", model: vm)); } }
public PartialViewResult RecentUnreadMessages() { int count = 10; string userId = null; if (User != null) { userId = User.Identity.GetUserId(); } using (var db = new ZapContext()) { var userMessages = db.Users .Where(u => u.AppId == userId) .SelectMany(u => u.Messages) .Where(m => !m.IsRead && !m.IsDeleted) .OrderByDescending(m => m.TimeStamp) .Take(count) .Select(m => new UserMessageVm() { FromName = m.From == null ? "" : m.From.Name, FromAppId = m.From == null ? "" : m.From.AppId, Id = m.Id, FromProfileImageVersion = m.From == null ? 0 : m.From.ProfileImage.Version, IsComment = m.CommentLink != null, PostId = m.PostLink == null ? 0 : m.PostLink.PostId, IsPrivateMessage = m.IsPrivateMessage, Content = m.Content, }) .AsNoTracking() .ToList(); var vm = new RecentUnreadMessagesViewModel() { MessagesVm = userMessages, }; return(PartialView("_PartialRecentUnreadMessages", model: vm)); } }