IQueryable being 3 times as slow with Contain-method vs Array

元气小坏坏 提交于 2020-01-17 05:46:47

问题


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?

来源:https://stackoverflow.com/questions/34002729/iqueryable-being-3-times-as-slow-with-contain-method-vs-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!