expression-trees

Lambda and Expression.Call for an extension method

感情迁移 提交于 2019-12-19 17:38:51
问题 I need to implement an expression for a method like here: var prop = Expression.Property(someItem, "Name"); var value = Expression.Constant(someConstant); var contains = typeof(string).GetMethod("Contains", new[] {typeof(string)}); var expression = Expression.Call(prop, contains, value); But for my extension method: public static class StringEx { public static bool Like(this string a, string b) { return a.ToLower().Contains(b.ToLower()); } } Unfortunately, next code throws an

How to call a lambda using LINQ expression trees in C# / .NET

十年热恋 提交于 2019-12-19 09:53:02
问题 I want to use expression trees to dynamically create a method to call a lambda. The following code runs fine for the first call to the ComposeLambda function, but the second call fails with the following error message. Incorrect number of arguments supplied for call to method 'Int32 lambda_method(System.Runtime.CompilerServices.Closure, Int32)' { Func<int, int> innerLambda = i => i + 1; var composedLambda = ComposeLambda(innerLambda); Console.WriteLine(composedLambda.DynamicInvoke(0)); var

The parameter '***' was not bound in the specified LINQ to Entities query expression

拜拜、爱过 提交于 2019-12-19 02:47:24
问题 I am doing a common query in my project. I use Expression to build my query tree, the code list below: public IList<Book> GetBooksFields(string fieldName, string fieldValue) { ParameterExpression paramLeft = Expression.Parameter(typeof(string), "m." + fieldName); ParameterExpression paramRight = Expression.Parameter(typeof(string), "\"" + fieldValue + "\""); ParameterExpression binaryLeft = Expression.Parameter(typeof(Book),"m"); BinaryExpression binaryExpr = Expression.Equal(paramLeft,

MemberExpression: InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined

≯℡__Kan透↙ 提交于 2019-12-19 02:02:04
问题 I'm using System.Linq.Expressions I was attempting to build a simple LambdaExpression that includes a MemberExpression. If I create the MemberExpression explicitly with the System.Linq.Expressions API (e.g. MakeMemberAccess), I will get the error "InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined" when I call Compile() on the LambdaExpression. For example ,this is my code Expression<Func<Customer, string>> expression1, expression2, expression3; Func

How to obtain ToTraceString for IQueryable.Count

不想你离开。 提交于 2019-12-18 19:32:28
问题 I use ((ObjectQuery)IQueryable).ToTraceString() to obtain and tweak SQL code that is going to be executed by LINQ. My problem is that unlike most IQueryable methods IQueryable.Count as defined like this: public static int Count(this IQueryable source) { return (int)source.Provider.Execute( Expression.Call( typeof(Queryable), "Count", new Type[] { source.ElementType }, source.Expression)); } executes query without compiling and returning IQueryable. I wanted to do the trick by something like

Expression tree for child collection List<string>.Any

巧了我就是萌 提交于 2019-12-18 17:00:44
问题 I am building generic linq query using expression tree. I am stuck when creating expression on child collection. Method call blows up because of incompatible types. Normally I know what to put there, but the Any() method call has me confused. I've tried every type I can think of and no luck. Any help would be appreciated. Here is my entity class: public class Story : Entity { public string Author { get; set; } public IList<string> Contributors { get; set; } } Query for which I want to

How to create an Expression tree to do the same as “StartsWith”

[亡魂溺海] 提交于 2019-12-18 15:53:04
问题 Currently, I have this method to compare two numbers Private Function ETForGreaterThan(ByVal query As IQueryable(Of T), ByVal propertyValue As Object, ByVal propertyInfo As PropertyInfo) As IQueryable(Of T) Dim e As ParameterExpression = Expression.Parameter(GetType(T), "e") Dim m As MemberExpression = Expression.MakeMemberAccess(e, propertyInfo) Dim c As ConstantExpression = Expression.Constant(propertyValue, propertyValue.GetType()) Dim b As BinaryExpression = Expression.GreaterThan(m, c)

How to create an Expression tree to do the same as “StartsWith”

此生再无相见时 提交于 2019-12-18 15:52:01
问题 Currently, I have this method to compare two numbers Private Function ETForGreaterThan(ByVal query As IQueryable(Of T), ByVal propertyValue As Object, ByVal propertyInfo As PropertyInfo) As IQueryable(Of T) Dim e As ParameterExpression = Expression.Parameter(GetType(T), "e") Dim m As MemberExpression = Expression.MakeMemberAccess(e, propertyInfo) Dim c As ConstantExpression = Expression.Constant(propertyValue, propertyValue.GetType()) Dim b As BinaryExpression = Expression.GreaterThan(m, c)

Get the parameter value from a Linq Expression

﹥>﹥吖頭↗ 提交于 2019-12-18 14:16:36
问题 I have the following class public class MyClass { public bool Delete(Product product) { // some code. } } Now I have a helper class that looks like this public class Helper<T, TResult> { public Type Type; public string Method; public Type[] ArgTypes; public object[] ArgValues; public Helper(Expression<Func<T, TResult>> expression) { var body = (System.Linq.Expressions.MethodCallExpression)expression.Body; this.Type = typeof(T); this.Method = body.Method.Name; this.ArgTypes = body.Arguments

How to get a value out of a Span<T> with Linq expression trees?

与世无争的帅哥 提交于 2019-12-18 13:05:51
问题 I would like to use Linq expression trees to call the indexer of a Span<T> . The code looks like: var spanGetter = typeof(Span<>) .MakeGenericType(typeof(float)).GetMethod("get_Item"); var myFloatSpan = Expression.Parameter(typeof(Span<float>), "s"); var myValue = Expression.Call( myFloatSpan, spanGetter, Expression.Constant(42)); var myAdd = Expression.Add( myValue, Expression.Constant(13f)); Yet, this code fails because myValue is of type Single& (aka ref struct ) instead of type Single