Team team = ctx.Teams
.Include("Login")
.Include("People")
.Include("School")
.Where(x => x.id == id
&& this.schoolsList.Contains(x.school_id))
.FirstOrDefault();
With this line you are retrieving the first element of ctx.Teams
that meets the requirements imposed by the Where
clause
ctx.Teams
Here you calling the table Teams
from the database through ctx
The Include
statements are used to join other tables like a Join in SQL and retrieves the data within the joined table.
.Where(x => x.id == id
&& this.schoolsList.Contains(x.school_id))
Here you are filtering the table data where the line has the id from the variable id
and is inside schoolsList
.
FirstOrDefault();
this retrieves the first item inside the IQueryable
returned by the Where
clause.
This can also be translated to:
Team team = ctx.Teams
.Include(x => x.Login)
.Include(x => x.People)
.Include(x => x.School)
.FirstOrDefault(x => x.id == id
&& this.schoolsList.Contains(x.school_id));
Includes written like this are less buggy like and more OOP. To do this you need the following namespace System.Data.Entity
.
** EDIT 1 **
I sort of understand but not completely. How does it know what
x.school_id is? I thought I had included it in my question, but I just
edited it in, and the only argument passed into that function was
"id." So where exactly is the value of x.school_id passed into
Contains coming from? Is it from the Team object that is returned from
the condition x.id == id? – FrostyStraw
Because the Where
clause or FirstOrDefault
does a iteration with SQL through ctx.Teams
where the class of Teams
contains the property school_id
.
This is only possible with EntityFramework, where a table is represented by a class and the class properties are table Columns.
Ah! and when you do this.schoolsList.Contains(x.school_id)
you are calling the list schoolsList
on each "SQL Iteration" caused by Where
.
It's like doing:
List<Team> teams = ctx.Teams
.Include("Login")
.Include("People")
.Include("School")
.ToList();
Team team = null;
foreach (var item in teams)
{
if (item.id == id && this.schoolsList.Contains(item.school_id))
{
team = item;
break;
}
}