/// <summary> /// 5.0 获取侧边栏最新评论,取6条. /// </summary> /// <returns>List集合</returns> public ActionResult NewComments() { IBLL.ICommentService commentService = OperateHelper.Current.serviceSession.CommentService; List <Comment> newCommentsList = commentService.GetDataListBy(c => c.Status != 0, c => c.SubTime); List <CommentViewModel> cmtViewModelList = new List <CommentViewModel>(); //视图模型 int j = 6; //取6条还是6条内 //如果最新评论说超过6条,则: if (newCommentsList.Count > 0) { if (newCommentsList.Count < 6) { j = newCommentsList.Count; } //newCommentsList = newCommentsList.Take<Comment>(j) as List<Comment>; newCommentsList = newCommentsList.OrderByDescending(c => c.SubTime).Take <Comment>(j).ToList(); string iconUrl; //评论所有人的头像路径 string defaultIconPath = ConfigurationManager.AppSettings["DefaultHeadIcon"]; for (int i = 0; i < j; i++) { //1.找评论所属游客的Id int?cmtVisitorId = newCommentsList[i].VisitorId; //Comment实体的VisitorId可空,因为被设计为:删除用户并不级联删除响应评论。 //2.如果这条评论所属的游客还存在数据库中 if (cmtVisitorId.HasValue) { //2.1 先找到它 int?headiconId = newCommentsList[i].Visitor.VisitorIconId; //2.2 如果这个游客对应的头像没被删除 if (headiconId.HasValue) { //找出这个头像地址 iconUrl = newCommentsList[i].Visitor.HeadIcon.IconURL; } else { iconUrl = defaultIconPath; //加上~才能显示吗?www.zynblog.com } } else { iconUrl = defaultIconPath; //否则就给这条评论加一个默认头像 } //3.0 构造视图模型 CommentViewModel viewmodel = new CommentViewModel() { CmtId = newCommentsList[i].Id, CmtText = newCommentsList[i].CmtText, CmtArtId = newCommentsList[i].CmtArtId, CmtIconUrl = iconUrl }; cmtViewModelList.Add(viewmodel); } } return(PartialView(cmtViewModelList)); }