Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efcore 3.1 does not support querying with string concatenation?

Tags:

c#

ef-core-3.1

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

like image 513
Maitland Marshall Avatar asked Feb 26 '20 08:02

Maitland Marshall


2 Answers

Try this

await this.Db.ACoolDbSet.Where(y => y.Plums.ToString() + " " + y.Pears.ToString() == "LOL").ToListAsync();
like image 183
KaMaHe Avatar answered Oct 03 '22 21:10

KaMaHe


string interpolation is working now in EF Core 3.1

await this.Db.ACoolDbSet.Where(y => $"{y.Plums} {y.Pear}" == "LOL").ToListAsync();
like image 40
Ram Avatar answered Oct 03 '22 21:10

Ram