efcore 3.1 does not support querying with string concatenation?

戏子无情 提交于 2020-05-14 01:23:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!