Entity Framework and SQL Server Profiler
c#, entity-framework, performance, sql-server-profiler
Solution
The venom in this query is in the first words: `IEnumerable<application>`. If you replace that by `var` (i.e. `IQueryable`) the query will be translated into SQL up to and including the last `Count()`. This will take considerably less time, because the amount of transported data is reduced to almost nothing.
Further, as bobek already mentioned, you don't need the `Include`s as you're only counting `context.applications` items.
Apart from that, you will always notice overhead of using an ORM like Entity Framework.
Problem
I have some performance measuring issue between the EF query run through the web application and running the Profiler generated T-SQL directly into the SQL Query window. Following is my EF query that executes through the web application: ``` IEnumerable<application> _entityList = context.applications .Include(context.indb_generalInfo.EntitySet.Name) .Include(context.setup_budget.EntitySet.Name) .Include(context.setup_committee.EntitySet.Name) .Include(context.setup_fund.EntitySet.Name) .Include(context.setup_appStatus.EntitySet.Name) .Include(context.appSancAdvices.EntitySet.Name) .Where(e => e.indb_generalInfo != null); if (isIFL != null) _entityList = _entityList.Where(e => e.app_isIFL == isIFL); int _entityCount = _entityList.Count(); // hits the database server at this line ``` While tracing the above EF Query in SQL Profiler it reveals that it took around 221'095 ms to execute. (The applications table having 30,000+, indb_generalInfo having 11,000+ and appSancAdvices having 30,000+ records). However, when I copy the T-SQL from Profiler and run it directly from Query window it takes around 4'000 ms only. Why is it so?