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

查看:24
本文介绍了在 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();
    ...

前 3 行有效,

然而,第四行 (bla3) 返回以下错误:

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

失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]执行请求 System.InvalidOperationException 时发生未处理的异常:当从VisitChildren"调用时,必须重写System.Linq.Expressions.Expression"类型的节点返回相同类型的非空值.或者,覆盖'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节点,字符串调用者名称)在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(WhereClausewhereClause, QueryModel queryModel, Int32 index) atRemotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection'1bodyClauses, QueryModel queryModel) 在Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel查询模型)在Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel查询模型)在Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel查询模型)在Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](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-测试版

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

[2 月 9 日 21:17] 克里斯汀·科尔

[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 Core:在带有变量的表达式中使用 Contains 方法产生了异常.

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

感谢您的错误报告.

这里是官方发布的帖子:

and here is the official release post:

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

修复了错误

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

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

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