title: Select years from datetime column returning new list with distinct years Linq To SQL vb.net
body: <p>I have a column with datetime and want to get the destinct years in a new list.</p>
  
  <p>The goal is to populate a DropDownList with the distinct years only (YYYY, not YYYY-MM-DD HH:MM:SS) extracted from the Games.Date column.</p>
  
  <p>Objects I have:</p>
  
  <pre><code>LDSYears (LinqDataSource)
  LDSUserResults (LinqDataSource)
  DropDownList1
  LDSYears_Selecting (LinqDataSourceSelectEvent)
  GridView1
  </code></pre>
  
  <p>I was thinking that I would use the LDSYears to populate the DropDownList1, then use the DropDownList1 as a Where statement in the LDSUserResults to filter only user results from the year that was selected in the DropDownList1.</p>
  
  <p>The LinqDataSourceSelectEvent is where the codes is located. The reason I have this code in the Event is that, what I have read it's not possible to use destinct in a LinqDataSource, and I also want to trim the output to year-only, not the datetime.</p>
  
  <pre><code>Protected Sub LDSYears_Selecting(sender As Object, _
          e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) _
      Handles LDSYears.Selecting
  
      Dim years = From dy In (From ys In db.Games _
                              Where ys.Date.Year.ToString &lt;&gt; "" Distinct _
                              Select ys.ID, ys.Date Distinct) _
                          Select dy _
                          Order By dy.Date Descending
      e.Result = years
  End Sub
  </code></pre>
  
  <p>Games.Date column:</p>
  
  <pre><code>2009-01-10 09:00:00
  2009-03-10 09:00:00
  2010-02-10 09:00:00
  2010-03-10 09:00:00
  2011-01-10 09:00:00
  2011-03-10 09:00:00
  </code></pre>
  
  <p>And I want a new list like this to be populated to my DropDownList, ID for index, and Years for Value:</p>
  
  <pre><code>ID Years
  1  2011
  2  2010
  3  2009
  </code></pre>
  
  <p>Maybe I'm looking at this at the wrong angle, It'should be possible?</p>
  
  <p>I can do this by code, but I thought it was a better way to use the DropDownList as a filter to LDSUserResults.</p>