//获得所关注用户的所有动态 //参数:当前用户id public List<UserActivity> getUserActivity(int id) { List<UserActivity> UA = new List<UserActivity>(); IEnumerable<FollowUser> users = db.FollowUsers.Where(d => d.FUUserID == id).ToList(); for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); temp.answer = new Answer(); int uID = temp.keyUser.UID; IEnumerable<Answer> tempA = db.Answers.Where(d => d.AUserID == uID); if (tempA.Count() == 0) continue; tempA = tempA.OrderByDescending(d => d.ATime); temp.answer = tempA.FirstOrDefault(d => d.AUserID == uID); temp.topic = null; temp.question = db.Questions.First(d => d.QID == temp.answer.AQuestionID); temp.time = temp.answer.ATime; temp._time = getTimeSpan(temp.time); temp.flag = 'a';//回答了某个问题 UA.Add(temp); } for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); int uID = temp.keyUser.UID; temp.question = new Question(); IEnumerable<Question> tempA = db.Questions.Where(d => d.QUserID == uID); if (tempA.Count() == 0) continue; tempA = tempA.OrderByDescending(d => d.QTime); temp.question = tempA.FirstOrDefault(d => d.QUserID == uID); temp.time = temp.question.QTime; temp._time = getTimeSpan(temp.time); temp.answer = null; temp.topic = null; temp.flag = 'q';//关注用户提了问题 if (temp.question != null) UA.Add(temp); } for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); int uID = temp.keyUser.UID; temp.question = null; temp.answer = null; IEnumerable<FollowTopic> ft = db.FollowTopics.Where(d => d.FTUserID == uID); if (ft.Count() == 0) continue; ft = ft.OrderByDescending(d => d.FTID); FollowTopic a = ft.FirstOrDefault(d => d.FTUserID == uID); if (a == null) continue; temp.topic = db.Topics.FirstOrDefault(d => d.TID == a.FollowingTID); if (temp.topic == null) continue; //TODO: 修改数据库后更改此处,应该是followTopic的时间 temp.time = a.FTTime; temp._time = getTimeSpan(temp.time); temp.flag = 't';//关注了话题 UA.Add(temp); } for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); temp.answer = null; IEnumerable<FollowQuestion> ft = db.FollowQuestions.Where(d => d.FQUserID == temp.keyUser.UID); if (ft.Count() == 0) continue; ft = ft.OrderByDescending(d => d.FQID); FollowQuestion fq = ft.FirstOrDefault(); if (fq == null) continue; temp.question = db.Questions.Find(fq.FollowingQID); //TODO: 修改数据库后更改此处,应该是followTopic的时间 temp.time = fq.FQTime; temp._time = getTimeSpan(temp.time); temp.flag = 'q';//关注了问题 UA.Add(temp); } return UA; }
//把UserActivity按照时间排序 public List<UserActivity> orderByTime(List<UserActivity> info) { UserActivity temp; for (int i = 0; i < info.Count(); i++) { for (int j = info.Count() - 1; j > i; j--) { if (info[j].time.ToOADate() > info[j - 1].time.ToOADate()) { temp = new UserActivity(); temp = info[j]; info[j] = info[j - 1]; info[j - 1] = temp; } } } return info; }
//获得所关注用户的所有动态 //参数:当前用户id public List <UserActivity> getUserActivity(int id) { List <UserActivity> UA = new List <UserActivity>(); IEnumerable <FollowUser> users = db.FollowUsers.Where(d => d.FUUserID == id).ToList(); for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); temp.answer = new Answer(); int uID = temp.keyUser.UID; IEnumerable <Answer> tempA = db.Answers.Where(d => d.AUserID == uID); if (tempA.Count() == 0) { continue; } tempA = tempA.OrderByDescending(d => d.ATime); temp.answer = tempA.FirstOrDefault(d => d.AUserID == uID); temp.topic = null; temp.question = db.Questions.First(d => d.QID == temp.answer.AQuestionID); temp.time = temp.answer.ATime; temp._time = getTimeSpan(temp.time); temp.flag = 'a';//回答了某个问题 UA.Add(temp); } for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); int uID = temp.keyUser.UID; temp.question = new Question(); IEnumerable <Question> tempA = db.Questions.Where(d => d.QUserID == uID); if (tempA.Count() == 0) { continue; } tempA = tempA.OrderByDescending(d => d.QTime); temp.question = tempA.FirstOrDefault(d => d.QUserID == uID); temp.time = temp.question.QTime; temp._time = getTimeSpan(temp.time); temp.answer = null; temp.topic = null; temp.flag = 'q';//关注用户提了问题 if (temp.question != null) { UA.Add(temp); } } for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); int uID = temp.keyUser.UID; temp.question = null; temp.answer = null; IEnumerable <FollowTopic> ft = db.FollowTopics.Where(d => d.FTUserID == uID); if (ft.Count() == 0) { continue; } ft = ft.OrderByDescending(d => d.FTID); FollowTopic a = ft.FirstOrDefault(d => d.FTUserID == uID); if (a == null) { continue; } temp.topic = db.Topics.FirstOrDefault(d => d.TID == a.FollowingTID); if (temp.topic == null) { continue; } //TODO: 修改数据库后更改此处,应该是followTopic的时间 temp.time = a.FTTime; temp._time = getTimeSpan(temp.time); temp.flag = 't';//关注了话题 UA.Add(temp); } for (var i = 0; i < users.Count(); i++) { UserActivity temp = new UserActivity(); temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID); temp.answer = null; IEnumerable <FollowQuestion> ft = db.FollowQuestions.Where(d => d.FQUserID == temp.keyUser.UID); if (ft.Count() == 0) { continue; } ft = ft.OrderByDescending(d => d.FQID); FollowQuestion fq = ft.FirstOrDefault(); if (fq == null) { continue; } temp.question = db.Questions.Find(fq.FollowingQID); //TODO: 修改数据库后更改此处,应该是followTopic的时间 temp.time = fq.FQTime; temp._time = getTimeSpan(temp.time); temp.flag = 'q';//关注了问题 UA.Add(temp); } return(UA); }