expression-trees

Generate Multi-Parameter LINQ Search Queries with Run-time Specified Return Type

ぐ巨炮叔叔 提交于 2019-12-13 02:38:24
问题 Having spent a long time solving this problem, I wanted to share the solution. Background I maintain a large web application with the primary function of managing orders. It is an MVC over C# application using EF6 for data. There are LOTS of search screens. The search screens all have multiple parameters and return different object types. The Problem Every search screen had: A ViewModel with the search parameters A Controller method to handle the Search event A method to pull the correct data

DbContext get IQueryable for scalar system functions (Count, Any, Sum, Max)

久未见 提交于 2019-12-13 02:23:57
问题 I have DBContext with DbSet called Assignments. It's not a problem to create queryable for enumerable expressions and concatenated them, however I don't see the way to get IQueryable with deferred execution for functions like Count, Any, Max, Sum. Basically I want to have some IQueryable extension so I can execute it like this: IQueryable<int> query = myDbContext.SelectValue((ctx)=>ctx.Assignments.Where(...).Count()) .UnionAll(myDbContext.SelectValue((ctx)=>ctx.Assignments.Where(...).Count())

Linq: Dynamic Query Contruction: query moves to client-side

白昼怎懂夜的黑 提交于 2019-12-13 00:37:27
问题 I've been following with great interest the converstaion here: Construct Query with Linq rather than SQL strings with regards to constructing expression trees where even the table name is dynamic. Toward that end, I've created a Extension method, addWhere, that looks like: static public IQueryable<TResult> addWhere<TResult>(this IQueryable<TResult> query, string columnName, string value) { var providerType = query.Provider.GetType(); // Find the specific type parameter (the T in IQueryable<T>

Adding Conditionals & Functions to a Math Parser

寵の児 提交于 2019-12-12 20:17:14
问题 I have a binary tree based mathematical expression parser I built, which works great for 'normal' math, like: (3.5 * 2) ^ 1 / (1 << 6) . however, I would like to expand it a little to add a ternary selection operator, mirroring the one from C: {expr} ? {true-expr} : {false-expr} . I would also like to add functions, like sin(x) or ave(...) . I however have no clue to how the handle this (due to the way the evaluation works), nor can I find anything on the web that covers this, atleast in a

Creating expression trees from lambda

倾然丶 夕夏残阳落幕 提交于 2019-12-12 20:04:52
问题 I have a lambda expression: (x) => x.Visits++ At runtime, I want to translate this into the string: "set Visits = Visits + 1" or, potentially, if the underlying data store is different (like MongoDB) {$inc: {Visits : 1}} I think the way to do this is to use expression trees, but when I assign the lambda expression to an expression tree, I get "An expression tree may not contain an assignment operator". Is there any way to accomplish this short of writing a full up linq implementation that

Traverse a Linq Expression to set the value of a property field

*爱你&永不变心* 提交于 2019-12-12 17:07:14
问题 This is a very complicated question even though it looks simple. I think I would have to traverse the entire expression tree unless someone knows a better way. Let's say I have a user object class User { public UserAccount Account {get;set;} } class UserAccount { public Name {get;set;} } var user = new User() { Account = new UserAccount()}; How could I use linq expressions to set the property of Name SetValue(c => c.Account.Name, "Test"); 回答1: Well, you'd not only have to traverse the

How to manually build Expression which will return always true?

爷,独闯天下 提交于 2019-12-12 10:47:47
问题 I tried to create Expression, but failed. I want to build something like Expression<Func<typeof(type), bool>> expression = _ => true; My attempt: private static Expression GetTrueExpression(Type type) { LabelTarget returnTarget = Expression.Label(typeof(bool)); ParameterExpression parameter = Expression.Parameter(type, "x"); var resultExpression = Expression.Return(returnTarget, Expression.Constant(true), typeof(bool)); var delegateType = typeof(Func<,>).MakeGenericType(type, typeof(bool));

Checked type-cast in an Expression Tree?

允我心安 提交于 2019-12-12 10:35:35
问题 I am using Expression to create a little bit of dynamically-generated code. My solution works, except for one feature: I want to do a checked type-cast, where TypeCastException is thrown if the cast fails. I have found Expression.TypeAs(), which does the type conversion, but it returns null, rather than throwing, when the cast fails. Is there a simple way to do a checked type-cast in Expression? Or do I have to check for null and throw the exception myself? Here's what I have: -

Splitting the expressions with Roslyn

懵懂的女人 提交于 2019-12-12 09:21:19
问题 I am using Roslyn and I want to split the statement as below, string stringVariable = "string"; int intVariable = 10; Console.Write(stringVariable + string.Concat("string1","string2") + intVariable.ToString()) Console.Write() stringVariable string.Concat("string1","string2") intVariable.ToString() I have asked a question and got answer for splitting the expressions Splitting the Expression statements with Roslyn but this suggestion splits the string.Concat("string1", "string2") as below,

How to pass compiler checked property names / Expression tree to a custom attribute

别说谁变了你拦得住时间么 提交于 2019-12-12 08:14:40
问题 In a few places, I've noticed expression trees passed as arguments to methods to allow compiler checking of property names. For example, Caliburn Micro has the following method signature in its PropertyChangedBase class: public virtual void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property); I have a custom attribute which I would like to have the same type of compiler checking of property names in the constructor, to enable me to type: [MyCustomAttribute(() =>