Actually I think you are not entirely correct - if the method returns
an IQueryable it does this at the DB level:
//extract:
var query =
IoC.Resolve<IRepository<Registration>>().Query();
var results = from reg in query
where
staff(person1).Any(x=>x == reg.Registrant)
select reg;
foreach (var r in results)
{
Console.WriteLine(
r.Registrant.UserAccount.Name);
}
}
private IQueryable<Person> staff(Person manager)
{
return
IoC.Resolve<IRepository<Person>>().Query().Where(p=>p.Supervisor==manager);
}
Even though staff is a method call, the sub-query does indeed do the
querying at the database level and not in code:
select registrati0_.RegistrationId as Registra1_40_,
registrati0_.PersonId as PersonId40_, registrati0_.ActivityId as
ActivityId40_,
registrati0_.CurrentResultId as CurrentR5_40_,
registrati0_.Status as Status40_, registrati0_.IsApproved as
IsApproved40_,
registrati0_.RegistrationType as Registra8_40_,
registrati0_.ApprovalDecidedOn as Approval9_40_,
registrati0_.CancelledOn as Cancell10_40_,
registrati0_.RegisteredOn as Registe11_40_, registrati0_.Note as
Note40_, registrati0_.CostAmount as CostAmount40_,
registrati0_.CancelledBy as Cancell14_40_,
registrati0_.CreatedBy as CreatedBy40_, registrati0_.CurrencyId as
CurrencyId40_,
registrati0_.ModifiedBy as ModifiedBy40_, registrati0_.CreatedOn
as CreatedOn40_, registrati0_.ModifiedOn as ModifiedOn40_,
registrati0_.BookingId as BookingId40_,
registrati0_.ActivityFlags as Activit21_40_,
registrati0_.REGISTRATION_CLASS as REGISTRA2_40_
from Registration registrati0_
where exists (
select person1_.PersonId from Person person1_
where person1_.SupervisorId=@p0 and
person1_.PersonId=registrati0_.PersonId);
@p0 = c085cd44-c49d-4ee5-b695-9eef00881c6d [Type: Guid (0)]
The bug - in my opinion (in terms of normal programming practice) is
because if you name the linq variables the same in the subquery method
as those in the calling query, you get a naming clash (e.g. if I used
x=>x instead of p=>p in the staff method).