Note that there are some explanatory texts on larger screens.

plurals
  1. PODatetime.Now cached in a .NET compiled LINQ query
    primarykey
    data
    text
    <p>Inside a compiled LINQ query as a condition part, I was using Datetime.Now . It used to give me irrelevant results. I think it used to store same Datetime.Now value for few subsequent calls. To solve this issue I send Datetime.Now as a value parameter to the compiled query and it is working fine.</p> <p><strong>Old Code</strong> </p> <pre><code> event.EventEnd &gt;= Datetime.Now </code></pre> <p><strong>New Code</strong> </p> <pre><code> event.EventEnd &gt;= currentTime </code></pre> <p>Where currentTime is a variable contains Datetime.Now value received outside from compiled query.</p> <p>Please let me know if this is the default behavior of complied LINQ query or I was wrong somewhere </p> <p>Platform : ASP.NET 4.0, MVC 2.0 </p> <p>Irrelevant results means , say we have one record with eVent.EventEnd value is 10-10-10 02:10 PM , if I run the query at 10-10-10 02:00 PM , we get that record in the result. If I run the query at 10-10-10 03:00 PM also we get that record in result. Which is wrong. But after I use the variable instead on DateTime.Now , it is working fine.</p> <p>Compiled LINQ Query</p> <pre><code>public static Func&lt;DataContext, CommonParams, string, DateTime, IQueryable&lt;EventEntity&gt;&gt; GetEventsByOwnerID = CompiledQuery.Compile( (DataContext context, CommonParams inputParams, string eventType, DateTime currentTime ) =&gt; (from eVent in context.Events join categories in context.Categories on eVent.CategoryID equals categories.CategoryID where !eVent.IsDeleted &amp;&amp; eVent.OwnerID == inputParams.UserID &amp;&amp; (eventType == "ALL" || (eventType == "CURRENT" &amp;&amp; eVent.EventEnd &gt;= currentTime) || (eventType == "OLD" &amp;&amp; eVent.EventEnd &lt; currentTime)) orderby eVent.PostedOn descending select new EventEntity() { EventID = eVent.EventID, CategoryID = eVent.CategoryID, Title = eVent.Title, Owner = eVent.OwnerName, EventStart = eVent.EventStart, EventEnd = eVent.EventEnd, Host = eVent.Host, Location = eVent.Location, Description = eVent.Description, Email = eVent.Email, URL = eVent.URL, Phone = eVent.Phone, CategoryName = categories.CategoryName, ProgramName = eVent.ProgramName, IsTelevised = eVent.IsTelevised, ChannelName = eVent.ChannelName, CityID = eVent.CityID, CountryID = eVent.CountryID, IsActive = eVent.IsActive, OwnerID = eVent.OwnerID, IsEndTimePartEmpty = eVent.IsEndTimePartEmpty, IsStartTimePartEmpty = eVent.IsStartTimePartEmpty, IsDeleted = eVent.IsDeleted, ThumbnailURL = eVent.ThumbnailURL, AttendeeCount = eVent.AttendeeCount, CommentsCount = eVent.CommentsCount }) ); </code></pre>
    singulars
    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.
 

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