// GET: Follows public ActionResult Index() { //ログインしてるユーザーID取得 string UserId = User.Identity.GetUserId(); //フォロー先ユーザーList作成 List <Follows> myFollows = db.Follows .Where(r => r.EmployeeId == UserId) .ToList(); // フォロー一覧リストを作成。 List <FollowsIndexViewModel> indexViewModels = new List <FollowsIndexViewModel>(); foreach (Follows follow in myFollows) { FollowsIndexViewModel indexViewModel = new FollowsIndexViewModel { Id = follow.Id, FollowName = db.Users.Find(follow.FollowId).EmployeeName, FollowId = follow.FollowId }; indexViewModels.Add(indexViewModel); } return(View(indexViewModels)); }
// GET: Follows public ActionResult Index() { //ログインユーザIDの取得 string MyId = User.Identity.GetUserId(); //ログインユーザがフォローしてる従業員IDのリスト //SELECT * FROM Follows WHERE EmployeeID == MyID var follows = db.Follows .Where(f => f.EmployeeId == MyId) .Select(f => f.FollowId) .ToList(); follows.Add(MyId); //reportsにフォローしているIDのレポートをリストで保持 //判定はfollowsのEmployeeIDと一致した件 var reports = db.Reports .Where(r => follows.Contains(r.EmployeeId)) .OrderByDescending(r => r.UpdatedAt) .ToList(); List <FollowsIndexViewModel> indexViewModels = new List <FollowsIndexViewModel>(); //reportsを分解してリストに追加 foreach (Report report in reports) { FollowsIndexViewModel indexViewModel = new FollowsIndexViewModel { //日報情報をindexViewModelに格納 Id = report.Id, EmployeeName = db.Users.Find(report.EmployeeId).EmployeeName, ReportDate = report.ReportDate, CliantCompany = report.CliantCompany, Title = report.Title, Content = report.Content, }; //レポートIDが自分でなければ if (MyId != report.EmployeeId) { //上記格納したデータをModelsに追加 indexViewModels.Add(indexViewModel); } } //Modelsをビューに戻す【向こう側のループで全件出力】 return(View(indexViewModels)); }