In our application we have a little query that looks like this:
var selectedAgents = from agent in listAgents
where (this.collectionVehicles.GetVehicleByAgent(agent)).IsActive ||
(this.collectionVehicles.GetVehicleByAgent(agent)).IsSoldSinceCurrentSession
select agent;
This works fine but the method GetVehicleByAgent is called two times and this is maybe not necessary.
Before LINQ we had a foreach-loop and did like this:
Vehicle parentVehicle = collectionVehicles.GetVehicleByAgent(agent);
if (parentVehicle.IsActive || parentVehicle.IsSoldSinceCurrentSession)
{
selectedAgents.Add(agent);
}
As you can see GetVehicleByAgent returns a Vehicle object and that can easiely compared more than once. So I am looking for a way the retieve the Vehicle inside the LINQ query and keep it. I don't want to call GetVehicleByAgent twice.