Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>(Replicating my comment to paste some sample code)</em></p> <p>If the date is in string format, couldn’t you apply a <code>ToString</code> on your <code>DateTime</code> (presumably <code>x</code>) and then do a string comparison? </p> <p>Since you are working on string representations, you need to take care of several issues that would otherwise be handled transparently by <code>DateTime</code>, including:</p> <ul> <li>Date-time format discrepancies (<code>dd/MM/yyyy</code> vs <code>MM/dd/yyyy</code>).</li> <li>Presence or absence of leading zeros for single-digit days and months (e.g. <code>01/01/2011</code> vs <code>1/1/2001</code>).</li> <li>Two-digit or four-digit representation of years (e.g. <code>01/01/2011</code> vs <code>01/01/11</code>).</li> <li>Timezone offsets. For example, the date for <code>2011-01-01 23:30 -01:00</code> would actually be <code>2011-01-02</code>.</li> </ul> <p>The sample code below will work if all your dates are in US format, with two-digit days and months, four-digit years, and no timezone offsets.</p> <pre><code>from t1 in Table1 where t1.Value.StartsWith(x.ToString(@"MM\/dd\/yyyy")) select new {t1.Col1, t1.Col2}; </code></pre> <p><strong>Edit</strong>: Alternate solution:</p> <p>If you can define a view in your database, you can circumvent the problem by casting your <code>VARCHAR</code> to <code>DATETIME</code> selectively. I’m assuming that <code>Value</code> is the name of your date column.</p> <pre><code>SELECT CONVERT(DATE, Value, 101) AS Value, Col1, Col2 FROM Table1 WHERE ISDATE(Value) = 1 </code></pre> <p>Then, in your LINQ, do a simple <code>DateTime</code> equality check:</p> <pre><code>from t1 in Table1 where t1.Value == x.Date select new {t1.Col1, t1.Col2}; </code></pre>
 

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