使用linq中的方法参数中的参数包含 [英] use parameter from method argument inside linq contains

查看:218
本文介绍了使用linq中的方法参数中的参数包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Contains()方法和来自方法参数的参数时遇到问题.

i have a problem using Contains() method with parameter coming from the method arguments.

我正在使用实体框架核心1.1和mysql连接器版本6.10.0-alpha.

i am using entity framework core 1.1 and mysql connector version 6.10.0-alpha.

我有这个代码:

public IEnumerable<Message> search(string content) {
    var bla = this.appDbContext.Messages.Where(x => x.Content.Contains("edit")).ToList();
    var bla1 = this.appDbContext.Messages.Where(x => x.Content=="edit").ToList();
    var bla2 = this.appDbContext.Messages.Where(x => x.Content==content).ToList();
    var bla3 = this.appDbContext.Messages.Where(x => x.Content.Contains(content)).ToList();
    ...

前三行有效,

但是,第四行(bla3)返回以下错误:

however, the fourth line (bla3) returns the following error:

失败: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware [0] 执行请求System.InvalidOperationException时发生未处理的异常:从"VisitChildren"调用时, 必须重写类型为'System.Linq.Expressions.Expression'的节点 返回相同类型的非null值.或者,覆盖 并更改"VisitChildren"以不访问此类儿童.

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0] An unhandled exception has occurred while executing the request System.InvalidOperationException: When called from 'VisitChildren', rewriting a node of type 'System.Linq.Expressions.Expression' must return a non-null value of the same type. Alternatively, override 'VisitChildren' and change it to not visit children of this type.

在 System.Linq.Expressions.ExpressionVisitor.VisitAndConvert [T](ReadOnlyCollection'1 节点,字符串callerName)位于 Microsoft.EntityFrameworkCore.Query.Expressions.SqlFunctionExpression.VisitChildren(ExpressionVisitor 访问者) Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitExtension(Expression 节点) Microsoft.EntityFrameworkCore.Query.Expressions.SqlFunctionExpression.Accept(ExpressionVisitor 访问者) Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ConditionalRemovingExpressionVisitor.Visit(Expression 节点) Microsoft.EntityFrameworkCore.Query.Expressions.LikeExpression.VisitChildren(ExpressionVisitor 访问者) Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitExtension(Expression 节点) Microsoft.EntityFrameworkCore.Query.Expressions.LikeExpression.Accept(ExpressionVisitor 访问者) Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ConditionalRemovingExpressionVisitor.Visit(Expression 节点) Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitWhereClause(WhereClause whereClause,QueryModel queryModel,Int32索引) Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection'1 bodyClauses,QueryModel查询模型),网址为 Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)位于 Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)位于 Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)位于 Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor [TResult](QueryModel queryModel) ---从之前引发异常的位置开始的堆栈跟踪---

at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection'1 nodes, String callerName) at Microsoft.EntityFrameworkCore.Query.Expressions.SqlFunctionExpression.VisitChildren(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitExtension(Expression node) at Microsoft.EntityFrameworkCore.Query.Expressions.SqlFunctionExpression.Accept(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ConditionalRemovingExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.Expressions.LikeExpression.VisitChildren(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitExtension(Expression node) at Microsoft.EntityFrameworkCore.Query.Expressions.LikeExpression.Accept(ExpressionVisitor visitor) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ConditionalRemovingExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index) at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection'1 bodyClauses, QueryModel queryModel) at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) --- End of stack trace from previous location where exception was thrown ---

为什么我不能使用Contains() linq表达式中的方法参数中的参数?

why can't i use parameter from the method arguments inside Contains() linq expression?

我该怎么做才能使用它?

and what can i do to be able to use it?

推荐答案

bug正式在6.10.1版中修复: https://www.nuget.org/packages/MySql.Data/6.10.1-beta

bug officially fixed in version 6.10.1: https://www.nuget.org/packages/MySql.Data/6.10.1-beta

https://bugs.mysql.com/bug.php?id=84505

[9 Feb 21:17]克里斯汀·科尔(Christine Cole)

[9 Feb 21:17] Christine Cole

开发者发布:

已在即将发布的MySQL Connector/NET 6.10.1版本中修复,这是更改日志条目:

Fixed as of the upcoming MySQL Connector/NET 6.10.1 release, and here's the changelog entry:

EF核心:在具有变量的表达式中使用Contains方法 产生异常.

EF Core: Using the Contains method in an expression with a variable generated an exception.

感谢您的错误报告.

这是官方发布的帖子:

http://insidemysql.com/mysql-connectornet-6-10-1-beta-has-been-released/

固定的错误

Bugs Fixed

  • EF Core:在包含变量的表达式中使用Contains方法会产生异常. (错误#25394204,错误#84505)

这篇关于使用linq中的方法参数中的参数包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆