Entity Framework throws NotSupportedException after calling Any or Count
c#, entity-framework, sql
Solution
You should use `.FirstOrDefault()` instead which will return `NULL` when there is no entities in dataset:
bool hasElement = dbe.FirstOrDefault() != null;
Another possible issue in your code is that you're using custom `IsEqualRecord` method which should contain only code that can be translated into SQL.
Problem
I've got the following code: ``` private void Import(DbSet<DBEntity> dbEntities, IEnumerable<ExcelEntity> entities, ClapEntities context) { foreach (var me in entities) { try { var dbe = dbEntities.Where(IsEqualRecord(me, context)); bool hasElement = dbe.Any(); //Line which throws the exception } catch (Exception ex) { //... } } } protected abstract Expression<Func<DBEntity, bool>> IsEqualRecord(ExcelEntity entity, ClapEntities context); ``` The `Any()` extension throws a `NotSupportedException`: System.Exception: Could not map entity to database. Mapper: [CuttingToolImport]; Rownumber: [153] ---> System.NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead. at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.FirstTranslator.TranslateUnary(ExpressionConverter parent, DbExpression operand, MethodCallExpression call) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression 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.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression 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.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.TypedTranslate(ExpressionConverter parent, MethodCallExpression 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.TypedTranslate(ExpressionConverter parent, MethodCallExpression 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_DisplayClassb.b_a() 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_DisplayClassb.b_9() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery`1..GetEnumerator>b__0() at System.Lazy`1.CreateValue() at System.Lazy`1.LazyInitValue() at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) at System.Linq.Queryable.Count[TSource](IQueryable`1 source) at CLAP.Models.Import.ImportMapping.ImportBase`2.Import(DbSet`1 dbEntities, IEnumerable`1 entities, ClapEntities context) --- End of inner exception stack trace --- As you can see, the `Any()` extension (or the `Count()` extension) uses the `First()` extension internal which is not allowed. I have no idea how I can fix that error. Exactly this code has worked for many months with Entity Framework version 5 or 6 and ``` Microsoft SQL Server 2012 - 11.0.2100.60 (X64) Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) ``` Does anyone has an idea on how to solve that error?