private void sendNotifyLikeDiscussions(DateTime now) { var requiredNotifyLikeDiscussions = _likeDiscussionRepo.GetRequireNotifyLikeDiscussions().ToList(); if (!requiredNotifyLikeDiscussions.Any()) { return; } var relatedCommentIds = requiredNotifyLikeDiscussions.Select(it => it.CommentId).Distinct(); var relatedDiscussions = _commentRepo.GetCommentById(relatedCommentIds) .Where(it => !it.DeletedDate.HasValue) .SelectMany(it => it.Discussions) .Where(it => !it.DeletedDate.HasValue) .ToList(); const string Message = "Like your discussion."; var notifyDiscussions = requiredNotifyLikeDiscussions.Select(it => { var discussion = relatedDiscussions.FirstOrDefault(d => d.id == it.DiscussionId); if (discussion == null) { return(null); } var isLikeSelfDiscussion = it.LikedByUserProfileId == discussion.CreatedByUserProfileId; if (isLikeSelfDiscussion) { return(null); } return(new Notification { id = Guid.NewGuid().ToString(), ByUserProfileId = new List <string> { it.LikedByUserProfileId }, ClassRoomId = it.ClassRoomId, CreatedDate = now, LastUpdateDate = now, LessonId = it.LessonId, Message = Message, Tag = Notification.NotificationTag.SomeOneLikesYourDiscussion, TotalLikes = 1, ToUserProfileId = discussion.CreatedByUserProfileId }); }).Where(it => it != null).ToList(); _notificationRepo.Insert(notifyDiscussions); requiredNotifyLikeDiscussions.ForEach(it => { it.LastNotifyComplete = now; _likeDiscussionRepo.UpsertLikeDiscussion(it); }); }