使用linq.dynamic生成的expression后报错 #1378
Replies: 3 comments 2 replies
-
func 具体类型是什么,#1113 这个可能有帮助 |
Beta Was this translation helpful? Give feedback.
-
func 就是Expression<Func<MyEntity,bool>>类型,具体错误堆栈如下: 这种方式生成的expression在ef core中是可以使用的,但在freesql中好像解析不了。 测试源码在https://gitee.com/tiansfather/fortest.git,您有空了麻烦帮忙看看 DynamicFilterInfo 还没用过,我去看看 |
Beta Was this translation helpful? Give feedback.
-
fsql.Aop.ParseExpression 解决办法如下:(v3.2.685 版本会集成) using FreeSql;
using FreeSql.DataAnnotations;
using System;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
var fsql = new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd => Console.WriteLine(cmd.CommandText))
.Build();
fsql.Aop.ParseExpression += (_, e) =>
{
if (e.Expression.NodeType == ExpressionType.Invoke &&
e.Expression is InvocationExpression invokeExp)
{
var replaceVistor = new FreeSql.Internal.CommonExpression.ReplaceVisitor();
var replaceExp = invokeExp.Expression;
var lambdaExp = replaceExp as LambdaExpression;
var len = Math.Min(invokeExp.Arguments.Count, lambdaExp.Parameters.Count);
for (var a = 0; a < len; a++)
replaceExp = replaceVistor.Modify(replaceExp, lambdaExp.Parameters[a], invokeExp.Arguments[a]);
e.Result = e.FreeParse(replaceExp);
}
};
//定义一个expression
Expression<Func<MyEntity, string>> newexp = x => x.Name;
//使用System.Linq.Dynamic.Core 包装成另一个expression
var func = DynamicExpressionParser.ParseLambda<MyEntity, bool>(new ParsingConfig(), true, "@0(it)==\"aa\"", new[] { newexp });
//报错variable '' of type 'MyEntity' referenced from scope '', but it is not defined
var a = fsql.Select<MyEntity>().Where(func).Count();
internal class MyEntity
{
[Column(IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
} |
Beta Was this translation helpful? Give feedback.
-
错误信息是:variable '' of type 'MyEntity' referenced from scope '', but it is not defined
有什么办法可以支持?
`using FreeSql;
using FreeSql.DataAnnotations;
using System;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
var fsql = new FreeSqlBuilder()
.UseConnectionString(DataType.MySql, "Server=localhost;Database=db;charset=utf8;user=root;password=root")
.UseAutoSyncStructure(true)
.Build();
Expression<Func<MyEntity, string>> newexp = x => x.Name;
var func = DynamicExpressionParser.ParseLambda<MyEntity, bool>(new ParsingConfig(), true, "@0(it)=="aa"", new[] { newexp });
//报错variable '' of type 'MyEntity' referenced from scope '', but it is not defined
var a = fsql.Select().Where(func).Count();
internal class MyEntity
{
[Column(IsIdentity = true)]
public int Id { get; set; }
}`
Beta Was this translation helpful? Give feedback.
All reactions