I have some records which I fetch from database (normally about 100-200). I also need to get the corresponding Place
for every record and fill in the Description
of the Place
in the record. I would normally do that in my .Select
function but I need to check if Place
isn't null before trying to take the Description
. My code goes like this:
var places = db.Places.Where(p => p.Active && p.CustomerID == cust_ID).ToArray();
foreach (var result in query)
result.Description =
places.Where(Place.Q.Contains(result.Latitude, result.Longitude).Compile())
.FirstOrDefault()?.Description;
query
is IQueryable
.
If I take places as IQueryable
or IEnumerable
and remove the Compile()
from my Expression, my code runs 3x (!!!) as slow as when I run the code as shown here.
Does anyone have an explanation for that? Does places
get fetched from database every loop of foreach?
(Edit as my first question was answered)
Also is there any way I could check if Place
is null in my Select
function (not taking the results into memory, keeping it IQueryable
) so I don't have to loop over my results afterwards?