如何将FirstOrDefault与Include一起使用?

时间:2015-09-07 10:41:24

标签: c# .net entity-framework linq

这很好用:

return Members
          .FirstOrDefault(m => m.Agreement.Equals(agreement));

但如果找不到匹配项,则抛出异常:

 return Members
            .Include("Files")
            .FirstOrDefault(m => m.Agreement.Equals(agreement) &&
            !m.Files.Any(f => f.Status.Equals(12)));

那么当我使用Include时,如何获得first / default(可能为null)?

例外是:

  

意外的异常   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter   parent,ConstantExpression linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 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 1.Translate(ExpressionConverter   父母,表达linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式   linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression   lambda,DbExpression输入)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression   lambda,DbExpression输入,DbExpressionBinding&绑定)   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter   parent,MethodCallExpression call,DbExpression&资源,   DbExpressionBinding&安培; sourceBinding,DbExpression& lambda)   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)
  在   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter   parent,MethodCallExpression linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 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 1.Translate(ExpressionConverter   父母,表达linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式   linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter   parent,BinaryExpression linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 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& binding) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& 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)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator
1.Translate(ExpressionConverter   父母,表达linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式   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)
  在   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter   parent,MethodCallExpression linq)at   System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 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()
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
1   forMergeOption)at   System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1   func,IDbExecutionStrategy executionStrategy,Boolean   startLocalTransaction,Boolean releaseConnectionOnSuccess)at   System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1   操作)   System.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable 1   forMergeOption)at   System.Data.Entity.Core.Objects.ObjectQuery 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator 1.MoveNext()at   System.Linq.Enumerable.Single [TSource](IEnumerable的1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable 1   序列)   System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle [TResult](IEnumerable的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 1   源)

3 个答案:

答案 0 :(得分:9)

这个怎么样:

return Members
    .Include("Files")
    .Where(m => m.Agreement.Equals(agreement) &&
                m.Files != null &&
               !m.Files.Any(f => f.Status.Equals(12)))
    .FirstOrDefault();

答案 1 :(得分:0)

尝试这种方式:

return Members.Include("Files")
                .FirstOrDefault(m => m.Agreement.Equals(agreement)
                 &&
                !m.Files.Any(f => f.Status==12));

我认为这会对你有帮助。

答案 2 :(得分:0)

使用Microsoft.EntityFrameworkCore 3.1.7可以正常工作:

var myEntity = await dbContext.MyEntities.Include(x => x.MyOtherEntities).FirstOrDefaultAsync(x => x.Name == "Not existing name");