title: How do you use FirstOrDefault with Include?
body: <p>This works fine:</p>
  
  <pre><code>return Members
            .FirstOrDefault(m =&gt; m.Agreement.Equals(agreement));
  </code></pre>
  
  <p>But this throws an exception if it doesn't find a match:</p>
  
  <pre><code> return Members
              .Include("Files")
              .FirstOrDefault(m =&gt; m.Agreement.Equals(agreement) &amp;&amp;
              !m.Files.Any(f =&gt; f.Status.Equals(12)));
  </code></pre>
  
  <p>So how can I get first/default (which may be null) when I'm using an Include?</p>
  
  <p>The exception is:</p>
  
  <blockquote>
    <p>Unexpected Exception    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter
    parent, ConstantExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator<code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter
    parent, BinaryExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator</code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression
    lambda, DbExpression input)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression
    lambda, DbExpression input, DbExpressionBinding&amp; binding)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call, DbExpression&amp; source,
    DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call, SequenceMethod sequenceMethod)<br>
    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter
    parent, MethodCallExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator<code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NotTranslator.TypedTranslate(ExpressionConverter
    parent, UnaryExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator</code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter
    parent, BinaryExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator<code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression
    lambda, DbExpression input)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression
    lambda, DbExpression input, DbExpressionBinding&amp; binding)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call, DbExpression&amp; source,
    DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call, SequenceMethod sequenceMethod)<br>
    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter
    parent, MethodCallExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator</code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.AggregateTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter
    parent, MethodCallExpression call, SequenceMethod sequenceMethod)<br>
    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter
    parent, MethodCallExpression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator<code>1.Translate(ExpressionConverter
    parent, Expression linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression
    linq)    at
    System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()<br>
    at
    System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable</code>1
    forMergeOption)    at
    System.Data.Entity.Core.Objects.ObjectQuery<code>1.&lt;&gt;c__DisplayClass7.&lt;GetResults&gt;b__6()
    at
    System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func</code>1
    func, IDbExecutionStrategy executionStrategy, Boolean
    startLocalTransaction, Boolean releaseConnectionOnSuccess)    at
    System.Data.Entity.Core.Objects.ObjectQuery<code>1.&lt;&gt;c__DisplayClass7.&lt;GetResults&gt;b__5()
    at
    System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func</code>1
    operation)    at
    System.Data.Entity.Core.Objects.ObjectQuery<code>1.GetResults(Nullable</code>1
    forMergeOption)    at
    System.Data.Entity.Core.Objects.ObjectQuery<code>1.&lt;System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator&gt;b__0()
    at System.Data.Entity.Internal.LazyEnumerator</code>1.MoveNext()    at
    System.Linq.Enumerable.Single[TSource](IEnumerable<code>1 source)    at
    System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__3[TResult](IEnumerable</code>1
    sequence)    at
    System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable<code>1
    query, Expression queryRoot)    at
    System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression
    expression)    at
    System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression
    expression)    at System.Linq.Queryable.Count[TSource](IQueryable</code>1
    source)</p>
  </blockquote>