Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing date comparison in LINQ when querying SharePoint OData service
    text
    copied!<p><strong>ANSWERED: Go below to find my answer to this question.</strong></p> <p>I am trying to consume SharePoint 2010 OData from an ASP.NET MVC 3 project using LINQ. I created a default project using the ASP.NET MVC 3 project template with the Razor view engine (VS 2010). I added a service reference pointing to my SharePoint 2010 site.</p> <p>In my HomeController's Index method (this is just a test project), I created a variable to hold the context and set the Credentials property of the context variable to the current default credentials.</p> <p>A link query like the following works fine and I can use the created variable to access any of the data:</p> <pre><code>var query = from a in context.Alerts select a; </code></pre> <p>This query simply gets all of the announcements from a list called Alerts in the SharePoint site. This list has fields for the Title, Content, Beginning Date, and Expiration Date.</p> <p>When I change the query to the following, I do not get the expected results:</p> <pre><code>var query = from a in context.Alerts where (a.Begins &lt; DateTime.Now) select a; </code></pre> <p>This query ignores the time component of the date. For example, if a.Begins contains a datetime from yesterday, the query returns the AlertItem. If on the other hand, a.Begins contains a datetime with the current date (but an earlier time) the comparison returns false (and a.Begins == DateTime.Now returns true).</p> <p>If I do the following, the second LINQ query works as expected:</p> <pre><code>var query = (from a in context.Alerts select a).ToList(); var query2 = from q in query where (q.Begins &lt; DateTime.Now) select q; </code></pre> <p>What am I missing?</p>
 

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