问题
Is there any way to query with EFCore 3.1 by joining multiple fields together with String.Format, or $"{}" or just the traditional "" + "" + ""?
I have this code:
await this.Db.ACoolDbSet.Where(y => y.Plums + " " + y.Pears == "LOL").ToListAsync();
Plums
and Pears
are integers.
It results in this error:
System.InvalidOperationException: 'Null TypeMapping in Sql Tree'
Is this expected?
This exception was originally thrown at this call stack:
Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.SqlTypeMappingVerifyingExpressionVisitor.VisitExtension(System.Linq.Expressions.Expression)
System.Linq.Expressions.Expression.Accept(System.Linq.Expressions.ExpressionVisitor)
System.Linq.Expressions.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlBinaryExpression.VisitChildren(System.Linq.Expressions.ExpressionVisitor)
System.Linq.Expressions.ExpressionVisitor.VisitExtension(System.Linq.Expressions.Expression)
Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.SqlTypeMappingVerifyingExpressionVisitor.VisitExtension(System.Linq.Expressions.Expression)
System.Linq.Expressions.Expression.Accept(System.Linq.Expressions.ExpressionVisitor)
System.Linq.Expressions.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(System.Linq.Expressions.Expression)
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(System.Linq.Expressions.Expression)
...
[Call Stack Truncated]
Adding y.Plums.ToString() and y.Pears.ToString() resolves the issue. String.Format and $"{}" are still not working unfortunately
回答1:
Try this
await this.Db.ACoolDbSet.Where(y => y.Plums.ToString() + " " + y.Pears.ToString() == "LOL").ToListAsync();
回答2:
string interpolation is working now
await this.Db.ACoolDbSet.Where(y => $"{y.Plums} {y.Pear}" == "LOL").ToListAsync();
来源:https://stackoverflow.com/questions/60409797/efcore-3-1-does-not-support-querying-with-string-concatenation