expression-trees

Out of Memory Lambda Compile versus inline delegates

岁酱吖の 提交于 2020-01-06 14:35:10
问题 Using 4.5.1 with an application that on the server side shuffles chart data with many REST requests simultaneously. Use IQueryable to build queries. For example, I originally had the following: var query = ctx.Respondents .Join( ctx.Respondents, other => other.RespondentId, res => res.RespondentId, (other, res) => new ChartJoin { Respondent = res, Occasion = null, BrandVisited = null, BrandInfo = null, Party = null, Item = null } ) . // bunch of other joins filling out the ChartJoin .Where(x

How to create Expression

ぃ、小莉子 提交于 2020-01-06 06:08:54
问题 I have 3 variables: String propertyName = "Title"; String propertyValue = "Bob"; Type propertyType = typeof(String); How can I construct Expression <Func<T, bool>> , if T object has property Title ? I need Expression: item => item.Title.Contains("Bob") if propertyType is bool, then I need item => item.OtherOproperty == false/true and so on... 回答1: This code performs filtering and stores results in filtered array: IQueryable<T> queryableData = (Items as IList<T>).AsQueryable<T>(); PropertyInfo

Building OrderBy-expression at runtime by property-name which can be nested

人盡茶涼 提交于 2020-01-05 07:36:20
问题 How can I dynamically build an order-by expression when only knowing the name of the property (or even the name of a sub-property)? What I'm trying to achieve is something like: dbResult = // some database-query as IQueryable<TSource> which is not yet executed; if (!string.IsNullOrEmpty(request.OrderBy)) { // the user want's to group the results var grouped = dbResult.GroupBy(/* this should be build dynamically */); } I need something to start with as GroupBy is awaiting a Func<TSource, TKey>

How to apply generic expression-trees code that works well on integers, to enums (e.g. how to make Expression.PostIncrementAssign() work on enums?)?

你离开我真会死。 提交于 2020-01-05 04:44:05
问题 I'm trying to create an extension method to do this: enum AlphaBet { A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z } IEnumerable<AlphaBet> rangeCtoG = AlphaBet.C.RangeToInc(AlphaBet.G); But this won't compile (as TEnum is generic): public static IEnumerable<TEnum> RangeToInc<TEnum>(this TEnum from, TEnum to) where TEnum : struct, IComparable, IFormattable, IConvertible // { for (; from <= to; from++) yield return from; } So I turned to expressions: public delegate void MyFunc<T>(ref T

ExpressionTree rewrite - MakeMemberAccess() for Navigation Properties

两盒软妹~` 提交于 2020-01-04 06:39:51
问题 Related vaguely to a previous question Note : I'm using a derivative of the ExpressionTree visitor as explained here In my VisitMemberAccess method I currently create MemberExpressions using something along the lines of: // `mapping` is a class used to map EntityA's members to EntityB's members return Expression.MakeMemberAccess(Visit(m.Expression), mapping.TargetMemberInfo); For the most part, this works. Given some test classes... public class EntityA { public long Id { get; set; } public

Expression Tree with string assignment and getting value

不羁岁月 提交于 2020-01-03 16:16:07
问题 I have built my own SQL Query builder that breaks apart an Expression, however, I'm having an issue trying to get the value of string defined in the same function as the lambda expression. Here is what I am trying to do in console app: private static void MyBuilderTest() { var sqlBuilder = new SqlBuilder(); // Doesn't work -- NEED GUIDANCE HERE var testValue = "Test"; // Defined in the same function as the lambda below sqlBuilder.Select<FooObject>(o => o.FooValue == testValue); // Works var

How do I call methods with reference variables with Expression Trees

安稳与你 提交于 2020-01-03 12:59:43
问题 I am trying to figure out how to create an Expression that calls a method which has a reference parameter. Let me explain my question with a simple (but artificial) example. Consider the method: public static int TwiceTheInput(int x) { return x*2; } I can create an Expression to call the above method by doing something like: { var inputVar = Expression.Variable(typeof (int), "input"); var blockExp = Expression.Block( new[] {inputVar} , Expression.Assign(inputVar, Expression.Constant(10)) ,

Convert Expression Tree to String and vice versa for WCF

Deadly 提交于 2020-01-03 05:33:09
问题 I want to Convert Expression Tree to String and Visa versa for using in WCF client and server query expression convert expression tree to string for send to server and convert string to expression tree in server so need this converter seems System.Linq.Dynamic is good library for this purpose so if yes please show me an example for this converting if no please any one show me a way for solve this problem i dont know how System.Linq.Dynamic works for this goal (example please) ******* of

Expression.Call GroupBy then Select and Count()?

感情迁移 提交于 2020-01-03 02:57:50
问题 Using Expression trees, I would need to build a GroupBy in a generic way. The static method I'm going to use is the following: public static IQueryable<Result> GroupBySelector<TSource>(this IQueryable<TSource> source, String coloumn) { //Code here } The Result class has two property : public string Value { get; set; } public int Count { get; set; } Basically I'd like to build the following Linq query via Expression trees: query.GroupBy(s => s.Country).Select(p => new { Value = p.Key, Count =

Create dynamic LINQ query expression at runtime which translates into a parameterized SQL query

蹲街弑〆低调 提交于 2020-01-02 14:30:13
问题 I want to create my custom expression for IQueryable. Sample extension method: public static IQueryable<T> GetByIntTest<T>(this IQueryable<T> qSource, Expression<Func<T, int?>> field, int? value) { if (value == null) return qSource; ConstantExpression constantExpression = Expression.Constant(value, typeof(int?)); BinaryExpression binaryExpression = Expression.Equal(field.Body, constantExpression); var predicate = Expression.Lambda<Func<T, bool>>(binaryExpression, field.Parameters); return