expression-trees

call ToArray on LINQ select query

一世执手 提交于 2019-12-08 04:34:33
问题 I have this query: Dim test = result.GroupBy(Function(row) groupedindexes.Select( Function(grpindex) row(grpindex)).ToArray, comp) I'm building an expression tree. I have already build the part inside the GroupBy function and now I would like to call the ToArray method. Here is the code: Public Function Grouping(ByVal result As IEnumerable(Of Object()), ByVal groupedindexes As List(Of Integer), ByVal comparer As compare) As Expression Dim groupbyMethod = GetType(Enumerable).GetMethods

Convert Expression<Func<FromType>> to Expression<Func<ToType>>

我们两清 提交于 2019-12-08 02:05:08
问题 How can I make a generic helper method to convert the type used by a Func from one type to another within an Expression I have a Expression<Func<IEmployee, bool>> and I want to convert it to a Expression<Func<Employee, bool>>. The second Type always implements the first Type. A generic solution is what I am trying to achieve. Edit I have edited the question to be clearer. 回答1: Well, you could create an expression that casts and then forwards its argument to the original expression: Expression

How to set properties and nested property's properties with expression

时光总嘲笑我的痴心妄想 提交于 2019-12-08 01:46:45
问题 I googled the propblem and also search the SO. There is a ton of solutions that all of them (that I found) are not completed. Can you help me please, to set a class's properties and its nested property's properties, choosen by a lambda , using Reflection ? public class Parent { public class Child { public int Id { get; set; } } public string Name { get; private set; } public int Number {get; private set; } public Child Nested { get; set; } public Parent() { Nested = new Child(); } public Test

Accessing expression bodied members to build expression trees

China☆狼群 提交于 2019-12-08 01:28:42
问题 Trying to build an order by expression using expression trees. But I am unable to access an expression bodied property of the query result's class. This is the class structure: public class AssetFileRecord : IAuditable, IEntity, INavigateToCustomValues { public AssetFileRecord() { this.UpdatedTimeStamp = DateTime.UtcNow; } public AssetFileRecord GetRecord() { return this; } public Guid Id { get; set; } public int DisplayId { get; set; } public string AssetTagNumber { get; set; }

Dynamically Adding a GroupBy to a Lambda Expression

纵饮孤独 提交于 2019-12-07 23:01:33
问题 Ok, I'll admit that I don't entirely "get" lambda expressions and LINQ expression trees yet; a lot of what I'm doing is cutting and pasting and seeing what works. I've looked over lots of documentation, but I still haven't found the my "aha" moment yet. With that being said... I'm attempting to dynamically add a GroupBy expression to my Linq expression. I followed the question here: Need help creating Linq.Expression to Enumerable.GroupBy and tried to implement what I saw there. First off, I

Sort using Linq.Expressions.Expression

橙三吉。 提交于 2019-12-07 21:32:53
问题 I wrote this code that sorts an IQueryable<T> by the column sortColumn . I would like to extend it so that the entries that have the value of the column BirthDate equal to DateTime.Today would be placed first in the sort, but I just can't find or think of how to do the job. public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string sortColumn, bool asc) { var param = Expression.Parameter(typeof(T), "p"); var prop = Expression.Property(param, sortColumn); var exp = Expression

How does one create a .NET Expression with NodeType of ExpressionType.Index?

谁说我不能喝 提交于 2019-12-07 15:17:24
问题 I'm writing code that evaluates .NET Expression trees. I'm trying to create a C# 4 test to exercise my handling of an ExpressionType.Index , but I can't figure out how to create that type of expression through a LambdaExpression . No matter what I try, the expression comes out as an ExpressionType.Call or ExpressionType.ArrayIndex . For example: IList<int> myList = new ObservableCollection<int> { 3, 56, 8 }; Expression<Func<int>> myExpression = () => myList[3]; // myExpression.Body.NodeType =

Moving C# function to expression for use in Entity Framework / SQL Select

一世执手 提交于 2019-12-07 15:02:31
问题 I have some small C# functions and computed cols from views that I'd like to move to Expressions so that they execute directly against the datasource in native T/SQL. I can do this inline in the Select, but I'd like to move it to a common function for re-use and testing. var results = context .Products .Select(p => new StockDto { Stock = p.GoodStock - p.LiveStock // Real version is more complex. }); I've tried creating a function that returns an expression but C# attempts to assign the

Unable to create a compound Expression<Func<string, bool>> from a set of expressions

六眼飞鱼酱① 提交于 2019-12-07 15:00:49
问题 (Answer towards the bottom) I'm trying to build a system that combines Func<T, bool> delegates into an ExpressionTree that allows me to pass in a value (badValue in this case) and get a bool out if the predicates all return true and the binary operations are taken into account. This is my first time using Expression/ExpressionTrees, so please be gentle. I'm getting this error: ArgumentException: Expression of type 'System.Boolean' cannot be invoked on this line: collectAnswers = Expression

How do I Create an Expression Tree by Parsing Xml in C#?

别等时光非礼了梦想. 提交于 2019-12-07 13:20:16
问题 I am looking to create an expression tree by parsing xml using C#. The xml would be like the following: <Expression> <If> <Condition> <GreaterThan> <X> <Y> </GreaterThan> </Condition> <Expression /> <If> <Else> <Expression /> </Else> <Expression> or another example... <Expression> <Add> <X> <Expression> <Y> <Z> </Expression> </Add> </Expression> ...any pointers on where to start would be helpful. Kind regards, 回答1: using System.Linq.Expressions; //in System.Core.dll Expression BuildExpr