Reputation: 827
I have entities in the context (memory), because I loaded them with Include()s.
I have about 25 entities (tph), and I'm running the following query on each of them:
actTph.TrainingProgramHistory_ConditionAndLoadParameter.Where(x => x.Level == 10).Select(x => x.ConditionAndLoadParameter).ToArray();
TrainingProgramHistory_ConditionAndLoadParameter, and TrainingProgramHistory_ConditionAndLoadParameter.ConditionAndLoadParameters are in memory, because when these queries are executed, SQL-profile logs nothing.
Each TPH has about 20 TrainingProgramHistory_ConditionAndLoadParameter related entities.
Running this query 25 times (on my 25 TPH entities) takes about 3 seconds!
If I refactor to this:
List<ConditionAndLoadParameter> measuredCalps = new List<ConditionAndLoadParameter>();
foreach (TrainingProgramHistory_ConditionAndLoadParameter tphCalp in actTph.TrainingProgramHistory_ConditionAndLoadParameter)
{
if (tphCalp.Level == 10)
{
measuredCalps.Add(tphCalp.ConditionAndLoadParameter);
}
}
Then it runs in 100msec or so.
How is Linq to Entities this slow for in-memory objects? What am I doing wrong?
Upvotes: 0
Views: 120
Reputation: 827
OK, was my bad :(
The first snippet was in a method, which contained a log/trace-line which apparently used lock()-s inside it, and even though this method was called from the same thread, calling these locks slowed down the performance.
In this case the foreach and linq query produced about the same performance after removing the locks.
Upvotes: 1