title: FirstOrDefault() when retrieving a single field value
body: <p>This type of thing has always bugged me so I thought I'd seek out a "better way" from you smart people.</p>
  
  <p>I have a query where I want to return the value of a single field, which is an int value. </p>
  
  <pre><code>int thivalue = (from q in context.tablename    
      where q.ID == id
      orderby q.fieldname descending
      select q).FirstOrDefault().fieldname;
  </code></pre>
  
  <p>Problem is the query might return no results in which case I want <em>thisvalue</em> to be 0. </p>
  
  <p>Of course if there are no results I get an exception trying to access the nonexistent field. So it seems my choices are to 
  a) return the row (which I don't need), so I can test for null and go from there, or 
  b) wrap a try catch around it and set the value to 0 there, which seems kinda clunky.</p>
  
  <p>I thought maybe DefaultIfEmpty() would help me but it seems not if what I want is to end up with just the single value.</p>
  
  <p>So what is the proper way to do this? Am I just being stubborn about not wanting to return the entire row when I only need a single value from it?  </p>
  
  <p><strong>Addendum: (if anyone is interested)</strong> </p>
  
  <p>In his answer berkeleybross gave me two apparently equivalent choices. But only the second one gave me the correct result. Something seemed amiss with the OrderByDescending. Using Glimpse I looked at the query for each. </p>
  
  <pre><code>    var nextSequence =  db.PaneContents
                         .Where(q=&gt;q.QuizPaneID == quizPaneId)
                         .OrderByDescending(q=&gt;q.Sequence)
                         .Select (q=&gt;q.Sequence)
                         .DefaultIfEmpty()
                         .First();
  </code></pre>
  
  <p>yielded this query: </p>
  
  <pre><code>SELECT 
  CASE WHEN ([Limit1].[C1] IS NULL) THEN 0 ELSE [Limit1].[Sequence] END AS [C1]
  FROM ( SELECT TOP (1) 
      [Project1].[Sequence] AS [Sequence], 
      [Project1].[C1] AS [C1]
      FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
      LEFT OUTER JOIN  (SELECT 
          [Extent1].[Sequence] AS [Sequence], 
          cast(1 as tinyint) AS [C1]
          FROM [dbo].[PaneContents] AS [Extent1]
          WHERE [Extent1].[QuizPaneID] = 274 /* @p__linq__0 */ ) AS [Project1] ON 1 = 1
  )  AS [Limit1]
  </code></pre>
  
  <p>whereas </p>
  
  <pre><code>var nextSequence = (from q in db.PaneContents
                                  where q.QuizPaneID == quizPaneId
                                  orderby q.Sequence descending
                                  select q.Sequence).FirstOrDefault();
  </code></pre>
  
  <p>yielded this query: </p>
  
  <pre><code>SELECT TOP (1) 
  [Project1].[Sequence] AS [Sequence]
  FROM ( SELECT 
      [Extent1].[Sequence] AS [Sequence]
      FROM [dbo].[PaneContents] AS [Extent1]
      WHERE [Extent1].[QuizPaneID] = 274 /* @p__linq__0 */
  )  AS [Project1]
  ORDER BY [Project1].[Sequence] DESC
  </code></pre>
  
  <p>Looks like in the first example the OrderByDescending() got lost in translation somehow? 
  Anyway for my purposes DefaultIfEmpty() was not needed and the second example is more straightforward. I just thought it odd that it didn't work (by 'didn't work' I mean it selected the wrong value because it didn't sort descending) because it looked like it should. </p>