private void label2_Click(object sender, EventArgs e) { var user = Cache.Get <User>("user"); var selectedRows = table.SelectedCells[0].Value as string; using (var ctx = new Database.ApplicationContext()) { var allFriends = ctx.FriendDetails.Include(it => it.Friend).Where(it => it.UserId == user.UserId && it.Friend.AreFriends).Select(it => it.Friend) .ToList(); foreach (var friend in allFriends) { var fr = ctx.FriendDetails.FirstOrDefault(it => it.FriendId == friend.FriendId && it.User.Email == selectedRows); if (fr != null) { ctx.Friends.Remove(friend); ctx.SaveChanges(); } } } table.Columns.Clear(); table.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Surname", HeaderText = "Фамилия" }); table.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Firstname", HeaderText = "Имя" }); table.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Lastname", HeaderText = "Отчество" }); table.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = "Email", HeaderText = "Электронная почта" }); table.AutoGenerateColumns = false; using (var ctx = new ApplicationContext()) { var allFriends = ctx.FriendDetails.Include(it => it.Friend).Where(it => it.UserId == user.UserId && it.Friend.AreFriends).Select(it => it.Friend) .ToList(); var list = new List <User>(); foreach (var friend in allFriends) { list.AddRange(ctx.FriendDetails.Where(it => it.FriendId == friend.FriendId && it.UserId != user.UserId).Select(it => it.User).ToList()); } table.DataSource = list; } }
private void label1_Click(object sender, EventArgs e) { var user = Cache.Get <User>("user"); var selectedRows = Subscribers.SelectedCells; if (selectedRows.Count == 0) { Add_Failed_2.Visible = true; return; } Add_Sucess.Visible = true; var secondUser = selectedRows[0].Value as string; using (var ctx = new Database.ApplicationContext()) { var secUserList = ctx.FriendDetails.Where(it => it.User.Email == secondUser).ToList(); var userList = ctx.FriendDetails.Where(it => it.UserId == user.UserId).ToList(); foreach (var secUserDetail in secUserList) { if (userList.FirstOrDefault(it => it.FriendId == secUserDetail.FriendId) != null) { var friend = ctx.Friends.FirstOrDefault(it => it.FriendId == secUserDetail.FriendId); friend.AreFriends = true; ctx.Friends.Update(friend); ctx.SaveChanges(); break; } } } refreshTable(); }