Using Linq to select the first and last values I want to get just the first and last values in a date range. I have the following code:

  using (var myEntities = new dataEntities())
  {
      var myValues = (from values in myEntities.PointValues
                      where values.PointID == dataValue &amp;&amp; values.DataTime &gt;= fromDate &amp;&amp; values.DataTime &lt;= toDate
                      orderby values.DataTime
                      select new BarChartValue
                      {
                          Time = values.DataTime,
                          Value = values.DataValue
                      }).ToList();
      var myBarChart = new List&lt;BarChartValue&gt; { myValues.First(), myValues.Last() };
      return myBarChart;
  }


This seems inefficient to me as I select the entire date range values initially. I can't seem to find a way to put first and last into the select command. Surely there must be a way to do this?

In response to Svick.

So, if I do this:

      var firstValue = (from values in myEntities.PointValues
                      where values.PointID == dataValue &amp;&amp; values.DataTime &gt;= fromDate &amp;&amp; values.DataTime &lt;= toDate
                      orderby values.DataTime
                      select new BarChartValue
                      {
                          Time = values.DataTime,
                          Value = values.DataValue
                      }).First();
      var lastValue = (from values in myEntities.PointValues
                        where values.PointID == dataValue &amp;&amp; values.DataTime &gt;= fromDate &amp;&amp; values.DataTime &lt;= toDate
                        orderby values.DataTime
                        select new BarChartValue
                        {
                            Time = values.DataTime,
                            Value = values.DataValue
                        }).Last();


Is that the best way to do it? I don't have any profiling tools (or if I do, I'm unsure how to use them).

The solution as provided by svick is:

      var myValues = (from values in myEntities.PointValues
                      where values.PointID == dataValue &amp;&amp; values.DataTime &gt;= fromDate &amp;&amp; values.DataTime &lt;= toDate
                      orderby values.DataTime
                      select new BarChartValue
                          {
                              Time = values.DataTime,
                              Value = values.DataValue
                          });
      var myBarChart = new List&lt;BarChartValue&gt; { myValues.First(), myValues.OrderByDescending(p =&gt; p.Time).First() };


A lot of this comes from my lack of understanding of the difference between IQueryable, IEnumerable and List. Here is a good explanation that helped me understand a bit more of what's going on:

https://stackoverflow.com/questions/4844660/differences-between-iqueryable-list-ienumerator
What you could do is to remove the ToList() from your query. This would change your code from making a single query that returns many values to two queries, each returning a single value.