question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[net5.0] Sequence '[Microsoft.EntityFrameworkCore.Query.QueryRootExpression]' cannot be converted to SQL

See original GitHub issue

linq2db 3.1.6 linq2db.EntityFrameworkCore 5.0.2 Microsoft.EntityFrameworkCore 5.0.0 TargetFramework net5.0

var query = dbContext.SomeItems.Where(x => x.IsDeleted && x.Updated < dateTime).Take(20);

return dbContext.SomeItems
                .Where(x => query
                    .Select(y => y.PrimaryKeyStringId)
                    .Contains(x.PrimaryKeyStringId)
                )
                .ToLinqToDB()
                .DeleteAsync();
LinqToDB.Linq.LinqException : Sequence '[Microsoft.EntityFrameworkCore.Query.QueryRootExpression]' cannot be converted to SQL.
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertSequence(BuildInfo buildInfo, ParameterExpression param, Boolean throwExceptionIfCantConvert)
   at LinqToDB.Linq.Builder.WhereBuilder.Convert(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo, ParameterExpression param)
   at LinqToDB.Linq.Builder.MethodCallBuilder.Convert(ExpressionBuilder builder, BuildInfo buildInfo, ParameterExpression param)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertSequence(BuildInfo buildInfo, ParameterExpression param, Boolean throwExceptionIfCantConvert)
   at LinqToDB.Linq.Builder.TakeSkipBuilder.Convert(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo, ParameterExpression param)
   at LinqToDB.Linq.Builder.MethodCallBuilder.Convert(ExpressionBuilder builder, BuildInfo buildInfo, ParameterExpression param)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertSequence(BuildInfo buildInfo, ParameterExpression param, Boolean throwExceptionIfCantConvert)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertExpressionTree(Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.OptimizeExpressionImpl(Expression expr, HashSet`1 currentParameters)
   at LinqToDB.Linq.Builder.ExpressionBuilder.<>c__DisplayClass45_0.<OptimizeExpression>b__0(Expression e)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Linq.Builder.ExpressionBuilder.OptimizeExpression(Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertSelect(MethodCallExpression method)
   at LinqToDB.Linq.Builder.ExpressionBuilder.OptimizeExpressionImpl(Expression expr, HashSet`1 currentParameters)
   at LinqToDB.Linq.Builder.ExpressionBuilder.<>c__DisplayClass45_0.<OptimizeExpression>b__0(Expression e)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Expressions.Extensions.Transform2[T](IEnumerable`1 source, Func`2 func)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Linq.Builder.ExpressionBuilder.OptimizeExpression(Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertWhere(MethodCallExpression method)
   at LinqToDB.Linq.Builder.ExpressionBuilder.OptimizeExpressionImpl(Expression expr, HashSet`1 currentParameters)
   at LinqToDB.Linq.Builder.ExpressionBuilder.<>c__DisplayClass45_0.<OptimizeExpression>b__0(Expression e)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Expressions.Extensions.Transform2[T](IEnumerable`1 source, Func`2 func)
   at LinqToDB.Expressions.Extensions.Transform(Expression expr, Func`2 func)
   at LinqToDB.Linq.Builder.ExpressionBuilder.OptimizeExpression(Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder.ConvertExpressionTree(Expression expression)
   at LinqToDB.Linq.Builder.ExpressionBuilder..ctor(Query query, IDataContext dataContext, Expression expression, ParameterExpression[] compiledParameters)
   at LinqToDB.Linq.Query`1.CreateQuery(IDataContext dataContext, Expression expr)
   at LinqToDB.Linq.Query`1.GetQuery(IDataContext dataContext, Expression& expr)
   at LinqToDB.Linq.ExpressionQuery`1.GetQuery(Expression& expression, Boolean cache)
   at LinqToDB.Linq.ExpressionQuery`1.LinqToDB.Async.IQueryProviderAsync.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at LinqToDB.LinqExtensions.DeleteAsync[T](IQueryable`1 source, CancellationToken token)

image

It was ok before upgrade to 5.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Dec 4, 2020

I see. Will check. Could you please try the following as a workaround:

using var db = dbContext.CreateLinqToDBConnection();
var query = db.GetTable<SomeItem>.Where(x => x.IsDeleted && x.Updated < dateTime).Take(20);

return await db.GetTable<SomeItem>
                .Where(x => query
                    .Select(y => y.PrimaryKeyStringId)
                    .Contains(x.PrimaryKeyStringId)
                )
                .DeleteAsync();
0reactions
RouRcommented, Jan 18, 2021

thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linq to SQL cannot translate because ...
I have been trying to run a query where I want to group all the records by a specific property, then select the...
Read more >
SQL Queries - EF Core
Entity Framework Core allows you to drop down to SQL queries when working with a relational database. SQL queries are useful if the...
Read more >
Breaking changes in EF Core 5.0
Complete list of breaking changes introduced in Entity Framework Core 5.0.
Read more >
Breaking changes included in EF Core 3.x
When expressions in any other part of the query can't be converted to either SQL or a parameter, an exception is thrown. Why....
Read more >
.Net 6 Upgrade - Microsoft Q&A
Hi Team, We are updating the .Net Core 3.1 framework to .Net 6 then we are getting this error. System.TypeInitializationException: 'The type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found