expression-trees

Expression of type 'System.Int64' cannot be used for return type 'System.Object'

假如想象 提交于 2019-12-06 07:52:46
问题 I am trying to create an expression of the following form: e => e.CreationDate; CreationDate is of type long , however I want the expression to return an object instead. I want to use object as a return type because the expression is built dynamically at runtime based on a query paramater. The query parameter specifies the property to access in the expression, such as: > entities?order=creationDate > entities?order=score As you can see, I can order by different properties with different types

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

那年仲夏 提交于 2019-12-06 07:33:51
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. Well, you could create an expression that casts and then forwards its argument to the original expression: Expression<Func<IEmployee, bool>> source = ... var param = Expression.Parameter(typeof(Employee)); // Types the

Sort using Linq.Expressions.Expression

一世执手 提交于 2019-12-06 07:12:18
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.Lambda(prop, param); string method = asc ? "OrderBy" : "OrderByDescending"; Type[] types = new[] { q

Accessing expression bodied members to build expression trees

你。 提交于 2019-12-06 06:32:49
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; } [JObjectIgnore] public virtual Account Account { get; set; } public string AccountNumber => Account?.AccountNumber;

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

故事扮演 提交于 2019-12-06 04:40:26
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 Set<TValue>(Expression<Func<???> func, TValue value) { // find the property name from expression //

Linq to NHibernate extensibility for custom string query operations?

大兔子大兔子 提交于 2019-12-06 04:28:25
问题 I would like to be able to use custom string querying within my NHibernate Linq expressions. Let's say for example (and this is just an example) I would like to be able to select entities containing a property which is an anagram of a particular string: var myEntities = EntityRepository.AllEntities.Where(x => x.Description.IsAnagramOf('hits'); I imagine the steps involved in this process would be: Define a SQL Server UDF to determine whether two strings are anagrams. Define an extension

how to evaluate an Expression inside ExpressionVisitor?

不羁岁月 提交于 2019-12-06 04:21:40
问题 I need to use ExpressionVisitor to analyse an Expression before executing it. For my needs, i need to evaluate the right part of a Divide expression but i don't know how to do it. Here's a sample code that i have: internal class RulesChecker : ExpressionVisitor { private readonly object data; public RulesChecker(object data) { this.data = data; } protected override Expression VisitBinary(BinaryExpression node) { if (node.NodeType == ExpressionType.Divide) { var rightExpression = node.Right; /

Reverse of Expression<Func<T,TResult>>.Compile()?

半世苍凉 提交于 2019-12-06 03:43:30
Since we can: Expression<Func<int, bool>> predicate = x => x > 5; var result = Enumerable.Range(0,10).Where(predicate.Compile()); How can I: Func<int,bool> predicate = x => x > 5; Expression<Func<int,bool>> exp = predicate.Decompile(); That is, I want to get the corresponding Expression of the Func . Is it possible? There is no magic Decompile() for a delegate instance, short of deconstructing the IL (perhaps with mono.cecil). If you want an expression tree, you'll have to start with an expression tree, so have Expression<Func<int, bool>> througout. As an edge case, you can get basic method

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

走远了吗. 提交于 2019-12-06 03:42:01
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 expression instead of the result of the expression and won't compile. Using the following Expression variable

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

时光怂恿深爱的人放手 提交于 2019-12-06 02:57:59
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, using System.Linq.Expressions; //in System.Core.dll Expression BuildExpr(XmlNode xmlNode) { switch(xmlNode.Name) { case "Add": { return Expression.Add( BuildExpr(xmlNode.ChildNodes[0])