I am trying to do the same as what I would have done using SQL not in command in ef core.
var studentsForTeam = await api.GetAllStudentsByTeamId(item.TeamId);
SessionId = item.Id;
TeamId = item.TeamId;
List<SessionStudent> studentsToRemove = await api.GetALLStudentsSessions();
// we want to remove the students that have alreeady arrived for a session.
var alreadyAttendend = studentsToRemove.Where(x=>x.isAbesent==false &&
x.isArrived==true).Select(x => x.StudentId );
var studentsnotArrvied = studentsToRemove.Where(w=>w.isAbesent==false &&
!alreadyAttendend.Contains(w.Id) ).ToList();
//I only want this to produce buttons for students that have not already checked in.
CheckInButtonsStudents(studentsForTeam);
So For Example I have
Student Tables
Student Id | Description |
---|---|
117 | The Dr |
118 | Martha Jones |
I have Student Sessions Table
Season Id | Student ID | IsAbsent | Is Arrived |
---|---|---|---|
86 | 117 | False | True |
87 | 118 | True | false |
So what I need my query above to do is where Student Session Student Id exists in student session where isAbsent false and isArrived true to exclude it form the Students for team query.
This is my check in function
public async void CheckInButtonsStudents(ObservableCollection<Student> students)
{
string name = string.Empty;
checkinButtons.Children.Clear(); //just in case so you can call this code several times np..
foreach (var item in students)
{
var btn = new Button()
{
AutomationId = StudentId.ToString(),
BackgroundColor = Color.Blue,
TextColor = Color.White,
Text = item.FirstName " " item.Surname,
};
btn.BindingContext = item; // add this here
btn.Clicked = BtnStudents_Clicked;
checkinButtons.Children.Add(btn);
}
}
CodePudding user response:
So what I need my query above to do is where Student Session Student Id exists in student session where isAbsent false and isArrived true to exclude it form the Students for team query.
If I understand you correctly, you want all students from studentsForTeam
where the student id exists in the student sessions and the student session isArrived but not isAbsent?
We have that student sessions already in alreadyAttended
(isArrived && !isAbsent). So we can filter the studentsforTeam
for students with an id in alreadyAttended
.
var studentsForCheckIn = studentsForTeam
.Where(s => alreadyAttended.Contains(s.id))
.ToList();
CheckInButtonsStudents(studentsForCheckIn);
Then, I recommend you to simplify your code a bit:
// I think you have a typo here: use isAbsent instead of isAbesent
// Don't compare for == true or == false. !isAbsent is the same as isAbsent == false
var alreadyAttendend = studentsToRemove
.Where(x => !x.isAbsent && x.isArrived)
.Select(x => x.StudentId);
// Here you select students that are not absent AND not in alreadyAttendend.
// That's the same as not absent and not arrived. If you write it like that, it's easier to read
var studentsNotArrvied = studentsToRemove
.Where(w => !w.isAbsent && !w.isArrived).ToList();