Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将一个表达式树转换/嵌入到另一个表达式树中_C#_Nhibernate_Lambda_Expression Trees_Iqueryable - Fatal编程技术网

C# 将一个表达式树转换/嵌入到另一个表达式树中

C# 将一个表达式树转换/嵌入到另一个表达式树中,c#,nhibernate,lambda,expression-trees,iqueryable,C#,Nhibernate,Lambda,Expression Trees,Iqueryable,编辑:更详细的问题 我正在nHibernate中处理批处理操作,特别是针对查询中的,以克服SQL Server中2100参数的大小限制 为此,我使用此构造函数创建了一个类(这是一个非常简化的版本): 有人能给我指一下正确的方向吗?我们将非常感谢您的帮助。 问候, Yogesh.如果你想在不编译expr1的情况下生成expr2,你不能只使用内置的编译器转换。这就是你想要的: Expression<Func<IList<TValue>, TEntity, bool>&

编辑:更详细的问题

我正在nHibernate中处理批处理操作,特别是针对查询中的
,以克服SQL Server中2100参数的大小限制

为此,我使用此构造函数创建了一个类(这是一个非常简化的版本):

有人能给我指一下正确的方向吗?我们将非常感谢您的帮助。

问候,

Yogesh.

如果你想在不编译expr1的情况下生成expr2,你不能只使用内置的编译器转换。这就是你想要的:

Expression<Func<IList<TValue>, TEntity, bool>> expr1 = (l, e) => l.Contains(e.Id);
ParameterExpression param = Expression.Parameter(typeof(TEntity), "e");
Expresssion<Func<TEntity, bool>> expr2 = Expression.Lambda<Func<TEntity, bool>>(Expression.Invoke(expr1, Expression.Constant(values), param), param);
表达式expr1=(l,e)=>l.Contains(e.Id);
ParameterExpression param=表达式参数(typeof(tenty),“e”);
Expression expr2=Expression.Lambda(Expression.Invoke(expr1,Expression.Constant(values),param),param);

我正在发布我的答案,以便其他人可以从中受益

做我想做的最好的方法是使用
ExpressionVisitor
Expression.Lambda
并对整个表达式进行适当的修改。该类是3/3.5中的一个内部类,因此,如果您想在3/3.5中使用该类,则是该类的完整实现。网上有大量的教程可供参考

其次,任何想要获得解析和可视化表达式树的工具的人都应该在VS2008中获得
ExpressionTreeVisualizer
示例。您必须将该示例转换并编译为VS2010 for.NET4.0。帕特里克·斯马奇亚如何帮助你


上面提到的一点是,即使按照我在回答中解释的方式转换查询,nhibernate仍然抛出异常。只有当我尝试将查询转换为未来的查询并对其求值时,才会发生这种情况。无需将查询转换为future,它就可以正常工作。我正在寻找同样的解决方案。如果我发现了原因以及解决方案,我会相应地更新答案。

谢谢。让我走一点。有没有办法,我可以直接在原始expr
中替换
l
?我的意思是在不调用
Invoke`的情况下生成expr2?理论上可以,但必须重新创建整个表达式树并替换值<代码>表达式。调用
只创建一个
调用表达式
。对于这个确切的示例,您可以这样做来进行替换:
expr2=Expression.Lambda((expr1.Body作为MethodCallExpression)。Update(Expression.Constant(values)、expr1.Parameters[0])
并删除定义
param
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at NHibernate.Param.NamedParameterSpecification.SetEffectiveType(QueryParameters queryParameters)
at NHibernate.Param.ParametersBackTrackExtensions.ResetEffectiveExpectedType(IEnumerable`1 parameterSpecs, QueryParameters queryParameters)
at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.ResetEffectiveExpectedType(IEnumerable`1 parameterSpecs, QueryParameters queryParameters)
at NHibernate.Loader.Loader.CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session)
at NHibernate.Impl.MultiQueryImpl.AggregateQueriesInformation()
at NHibernate.Impl.MultiQueryImpl.get_Parameters()
at NHibernate.Impl.MultiQueryImpl.CreateCombinedQueryParameters()
at NHibernate.Impl.MultiQueryImpl.List()
at NHibernate.Impl.FutureQueryBatch.GetResultsFrom(IMultiQuery multiApproach)
at NHibernate.Impl.FutureBatch`2.GetResults()
at NHibernate.Impl.FutureBatch`2.get_Results()
at NHibernate.Impl.FutureBatch`2.GetCurrentResult[TResult](Int32 currentIndex)
at NHibernate.Impl.FutureBatch`2.<>c__DisplayClass4`1.<GetEnumerator>b__3()
at NHibernate.Impl.DelayedEnumerator`1.<get_Enumerable>d__0.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at NovusERP.Data.Helpers.BatchedQuery`2.ToList() in D:\Short Utilities\Novus\NovusERP\NovusERP.Data\Helpers\BatchedQuery.cs:line 63
at NovusERP.Modules.Payroll.Attendance.AttendanceViewModel.GetEmployees(IList`1 selectedEmployeeIds) in D:\Short Utilities\Novus\NovusERP\NovusERP.Modules\Payroll\Attendance\AttendanceViewModel.cs:line 79
at NovusERP.Modules.Payroll.Attendance.AttendanceViewModel..ctor(MonthYear currentMonth, IList`1 selectedEmployeeIds) in D:\Short Utilities\Novus\NovusERP\NovusERP.Modules\Payroll\Attendance\AttendanceViewModel.cs:line 47
at NovusERP.Modules.Payroll.Attendance.AttendanceView..ctor(MonthYear currentMonth, IList`1 selectedEmployees) in D:\Short Utilities\Novus\NovusERP\NovusERP.Modules\Payroll\Attendance\AttendanceView.xaml.cs:line 18
at lambda_method(Closure , Object[] )
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
Expression<Func<IList<TValue>, TEntity, bool>> expr1 = (l, e) => l.Contains(e.Id);
ParameterExpression param = Expression.Parameter(typeof(TEntity), "e");
Expresssion<Func<TEntity, bool>> expr2 = Expression.Lambda<Func<TEntity, bool>>(Expression.Invoke(expr1, Expression.Constant(values), param), param);