Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Entity Framework using class from model in a query slower than using anonymous class
    primarykey
    data
    text
    <p>I am using Entity Framework 5 to access my DB. The model is quite complex with a lot of navigation properties. I have written the following query using linq:</p> <pre><code>var myQuery = from cp in context.ClosedPositions.Include("Position").Include("Position.Folder").Include("Position.Strategy").Include("Position.Symbol").Include("Position.StopTargetPlacer") where cp.Position.EntryDate &gt;= fromDT &amp;&amp; cp.ExitDate &lt;= toDT &amp;&amp; (cp.Position.Folder.FolderCode == myFolder || showAllFolders) &amp;&amp; (cp.Position.Strategy.Name == myStrategy || showAllStrategies) &amp;&amp; (cp.Position.Symbol.Name == mySymbol || showAllSymbols) &amp;&amp; (cp.Position.Symbol.Exchange == myExchange || showAllExchanges) orderby cp.Position.EntryDate select cp; </code></pre> <p>The navigations multiplicity are the following:</p> <p>Position 1 - * ClosedPostion</p> <p>Position * - 1 Folder</p> <p>Position * - 1 Strategy</p> <p>Position * - 1 Symbol</p> <p>Then in a foreach query I use the Data from the Included navigation properties. I think this way should not hit the database more than once. The query runs for about 6 seconds.</p> <p>Then I have rewritten the query to this:</p> <pre><code>var myQuery = from cp in context.ClosedPositions join p in context.Positions on cp.PositionID equals p.ID join f in context.Folders on p.FolderID equals f.ID join sy in context.Symbols on p.SymbolID equals sy.ID join st in context.Strategies on p.StrategyID equals st.ID join stp in context.StopTargetPlacers on p.StopTargetPlacerID equals stp.ID where p.EntryDate &gt;= fromDT &amp;&amp; cp.ExitDate &lt;= toDT &amp;&amp; (f.FolderCode == myFolder || showAllFolders) &amp;&amp; (st.Name == myStrategy || showAllStrategies) &amp;&amp; (sy.Name == mySymbol || showAllSymbols) &amp;&amp; (sy.Exchange == myExchange || showAllExchanges) orderby p.EntryDate select new { ClosedPositionID = cp.ID, PositionID = p.ID, p.EntryChartID, cp.ExitChartID, p.EntryDate, cp.ExitDate, Symbol = sy.Name, Strategy = st.Name, p.Size, cp.Profit, STPlacer = p.StopTargetPlacer.Name, InitialRisk = p.InitialRisk, StrategyDirection = st.Direction }; </code></pre> <p>Again I have used the same foreach loop to work on the data. This time the total processing time was only around 1 second. </p> <p>I have examined the generated SQL queries by both LINQ query in running them in the SSMS both of them returned the same data in the same amount of time. </p> <p>My question is why is there the huge delay between using an anonymous class and a class from the model of the context?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload