expression-trees

Working with nullable types in Expression Trees

被刻印的时光 ゝ 提交于 2020-01-20 16:51:48
问题 I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here's my code: public static IOrderedQueryable<T> OrderingHelperWhere<T>(this IQueryable<T> source, string columnName, object value) { ParameterExpression table = Expression.Parameter(typeof(T), ""); Expression column = Expression.PropertyOrField(table, columnName); Expression where = Expression.GreaterThanOrEqual(column, Expression.Constant

Reading Properties of an Object with Expression Trees

旧城冷巷雨未停 提交于 2020-01-20 01:59:09
问题 I want to create a Lambda Expression for every Property of an Object that reads the value dynamically. What I have so far: var properties = typeof (TType).GetProperties().Where(p => p.CanRead); foreach (var propertyInfo in properties) { var getterMethodInfo = propertyInfo.GetGetMethod(); var entity = Expression.Parameter(typeof (TType)); var getterCall = Expression.Call(entity, getterMethodInfo); var lambda = Expression.Lambda(getterCall, entity); var expression = (Expression<Func<TType,

Reading Properties of an Object with Expression Trees

这一生的挚爱 提交于 2020-01-20 01:58:08
问题 I want to create a Lambda Expression for every Property of an Object that reads the value dynamically. What I have so far: var properties = typeof (TType).GetProperties().Where(p => p.CanRead); foreach (var propertyInfo in properties) { var getterMethodInfo = propertyInfo.GetGetMethod(); var entity = Expression.Parameter(typeof (TType)); var getterCall = Expression.Call(entity, getterMethodInfo); var lambda = Expression.Lambda(getterCall, entity); var expression = (Expression<Func<TType,

Using expression trees to create a custom order by in linq to entities

让人想犯罪 __ 提交于 2020-01-16 01:53:13
问题 I have a table that's mapped, but after compile additional columns can be added or removed from the table. I'm trying to come up with a linq query that will take those new columns into account. In this scenario, I want to order by one of those dynamic columns. This is what I have so far. var queryableData = dc.wf_task_ext_attributes.AsQueryable(); ParameterExpression pe = Expression.Parameter(typeof(DateTime), "ExtValue105"); // The next line is where it fails MethodCallExpression

How to get MemberInfo of ArrayLength type expressions?

佐手、 提交于 2020-01-16 01:10:25
问题 Some trouble with UnaryExpression s. This works this way: Expression<Func<List<string>, object>> k = l => l.Count; //got member in this case like this var member = ((k.Body as UnaryExpression).Operand as MemberExpression).Member; In the above case the k.Body.NodeType was ExpressionType.Convert . But it's a little tricky with ExpressionType.ArrayLength . How would I get the PropertyInfo member similarly in the below case?: Expression<Func<string[], int>> k = l => l.Length; var member = ?? In

Nested Lambda Expressions

旧街凉风 提交于 2020-01-15 12:22:13
问题 Does anyone have any idea how I call a lambda expression from within a lambda expression? If I have: public class CourseViewModel { public int Id { get; set; } public string Name { get; set; } public static Expression<Func<Course, CourseViewModel>> AsViewModel = x => new CourseViewModel { Id = x.Id, Name = x.Name, } } public class StudentViewModel { public int Id { get; set; } public string Name{ get; set; } public string PreferredCheese { get; set; } public IEnumerable<CourseViewModel>

How do I create a custom Select lambda expression at runtime to work with sub classes

混江龙づ霸主 提交于 2020-01-14 02:45:07
问题 If I have following type hierarchy: abstract class TicketBase { public DateTime PublishedDate { get; set; } } class TicketTypeA:TicketBase { public string PropertyA { get; set; } } class TicketTypeB:TicketBase { public string PropertyB { get; set; } } and many more TicketTypes : TicketBase and want to create a function which selects any property e.g. PropertyA from any ticket type e.g. TicketTypeA I wrote this function: private Func<TicketBase, String> CreateSelect(Type t, String FieldName) {

Dynamically creating an expression which selects an objects property

大城市里の小女人 提交于 2020-01-13 13:04:32
问题 I want to be able to build up an expression dynamically, which is essentially a property selector. I am trying to use this so I can provide a flexible search UI and then translate the selected search parameters to an Entity Framework query. I have most of what I need thanks to another library I am using, but am missing the final part which translates my query string parameters to the appropriate expression selector the other library requires. The library takes an argument of : Expression<Func

Dynamically creating an expression which selects an objects property

大兔子大兔子 提交于 2020-01-13 13:04:05
问题 I want to be able to build up an expression dynamically, which is essentially a property selector. I am trying to use this so I can provide a flexible search UI and then translate the selected search parameters to an Entity Framework query. I have most of what I need thanks to another library I am using, but am missing the final part which translates my query string parameters to the appropriate expression selector the other library requires. The library takes an argument of : Expression<Func

Implement Not in expression trees, .net 4

纵然是瞬间 提交于 2020-01-13 11:01:56
问题 Is it possible to implement a ! (not) using expression trees. I'm interested in creating a C# eval class which will parse and evaluate logical expressions which contain true, false, ||, && and !. I know that && and || are currently supported by .NET 4 expression trees, but I was wondering if their is a way to implement summat like !(x && y) || z where z=false, y=true and z=false. Currently I'm using a standard stack based tokenizer, parser, evaluator to evaluate these types of expression but