0

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?

Alexander Derck
  • 13,818
  • 5
  • 54
  • 76
  • 2
    Two questions here. The main one is a dupe. http://stackoverflow.com/questions/8107439/entity-framework-4-1-most-efficient-way-to-get-multiple-entities-by-primary-key . You might choose to rephrase things, because I'll be coming back to this with my dupehammer. The top answer in the link above is a superb answer. Well worth a thorough read. – spender Nov 30 '15 at 16:06
  • There's no way by reading the title of that question that I'd think it answers the question about `Contain`... – Alexander Derck Nov 30 '15 at 16:08
  • I only know about it because it was asked by a colleague of mine... – spender Nov 30 '15 at 16:11
  • "Does places get fetched from database every loop of foreach?" - Of course not - ToArray() fetch data only once, and after that u're querying to data in memory – Mitklantekutli Nov 30 '15 at 16:12
  • I know it gets fetched only once when I call `ToArray()`, but my point is that my program runs 3x slower If I don't call it and leave places as Queryable, so what's the reason for that? – Alexander Derck Nov 30 '15 at 17:08
  • @spender Very interesting read indeed, also a very clear approach of testing different methods, cheers for that!. Looks like the Linq-to-sql `.Contain()` method is a no-go. Does anyone know if there's any way I could check if `Place` is null in my select before taking the description? Maybe the best way is to define a model and pass the `Place` to a property. – Alexander Derck Nov 30 '15 at 18:12
  • @AlexanderDerck There's something fishy about your `.Where` statement above. Shouldn't there be a lambda in it? – spender Dec 01 '15 at 19:49
  • My `Place` class contains a `Q` class with expressions in it with lambdas I often use. (the .Contains of `Q` is an extension method, should've clarified that) – Alexander Derck Dec 01 '15 at 20:07

0 Answers0