expression-trees

Binding parameter in Expression trees

无人久伴 提交于 2019-12-04 11:17:26
问题 I would like to know how to bind parameters to values within an expression tree Something like Expression<Func<String, String, bool>> e1 = (x,y) => x == y; Then I would like to bind y, while preserving it as a single expression. A obvious attempt would be something like Expresion<Func<String, bool>> e2 = x => e1(x, "Fixed Value Here"); But that would turn my expression into an Invoke node. Is there a way to simply bind a parameter within my first expression while getting the signature of the

Can't find OrderBy on Queryable with the “supplied arguments”.

不问归期 提交于 2019-12-04 11:13:11
问题 I have a method that I want to use to sort a list: private static IQueryable<T> BuildQuery<T>(IQueryable<T> query, string methodName, Expression<Func<T, object>> property) { var typeArgs = new[] { query.ElementType, property.Body.Type }; methodCall = Expression.Call(typeof (Queryable), methodName, typeArgs, query.Expression, property); return query.Provider.CreateQuery<T>(methodCall); } I get an exception when I execute the code using the following args: var myPreExistingQuery = new List

Construct a LINQ GroupBy query using expression trees

给你一囗甜甜゛ 提交于 2019-12-04 11:09:00
问题 I have stuck on this problem for a week and no solution found. I have a POCO like below: public class Journal { public int Id { get; set; } public string AuthorName { get; set; } public string Category { get; set; } public DateTime CreatedAt { get; set; } } I want to know during a specific date span ( grouped by months or years ) the amount of journals count by a AuthorName or a Category. After I send the queryed object to JSON serializer then generated JSON data like below ( just using JSON

Linq to NHibernate extensibility for custom string query operations?

人走茶凉 提交于 2019-12-04 11:05:10
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 method called IsAnagramOf() for the String class. (And this is the tricky one). Modify Linq to NHibernate's

How can I transform this linq expression?

我的梦境 提交于 2019-12-04 09:21:07
Say I have an entity that I want to query with ranking applied: public class Person: Entity { public int Id { get; protected set; } public string Name { get; set; } public DateTime Birthday { get; set; } } In my query I have the following: Expression<Func<Person, object>> orderBy = x => x.Name; var dbContext = new MyDbContext(); var keyword = "term"; var startsWithResults = dbContext.People .Where(x => x.Name.StartsWith(keyword)) .Select(x => new { Rank = 1, Entity = x, }); var containsResults = dbContext.People .Where(x => !startsWithResults.Select(y => y.Entity.Id).Contains(x.Id)) .Where(x =

Generated methods for polynomial evaluation

独自空忆成欢 提交于 2019-12-04 08:16:07
I'm trying to come up with an elegant way to handle some generated polynomials. Here's the situation we'll focus on (exclusively) for this question: order is a parameter in generating an n th order polynomial, where n:=order + 1. i is an integer parameter in the range 0..n The polynomial has zeros at x_j, where j = 1..n and j ≠ i (it should be clear at this point that StackOverflow needs a new feature or it's present and I don't know it) The polynomial evaluates to 1 at x_i. Since this particular code example generates x_1 .. x_n, I'll explain how they're found in the code. The points are

Print out Linq Expression Tree Hierarchy

≡放荡痞女 提交于 2019-12-04 08:08:15
问题 The dynamic language runtime (DLR) has some pretty cool code for Expression's, including some very nice code to print out Expression trees which I want to use so that: int a = 1; int b = 2; Expression<Func<int, int>> expression = (c) => a + (b * c) expression.Evaluate(5, stringBuilder) Outputs: (5) => a + (b * c) = 11 Where a = 1 b * c = 10 Where b = 2 c = 5 I found some code on the net to do this but found that it only works if the expressiontakes in no arguments. http:/

Expression tree depth limitations

给你一囗甜甜゛ 提交于 2019-12-04 07:41:13
I'm facing a problem trying to call Compile() on the LambdaExpression of type Expression<Func<MyType, bool>> which has a depth around 400. And lesser values do not cause any problems. And I can't find anything about such kind of limitation. Can anyone clarify this? Can I increase this limit? upd: Sorry, forgot to mention, I'm getting StackOverflowException: An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll {Cannot evaluate expression because the current thread is in a stack overflow state.} You are legitimately running into a limit on the stack size

Are Roslyn SyntaxNodes reused?

℡╲_俬逩灬. 提交于 2019-12-04 07:27:14
问题 I've been taking a look to Roslyn CTP and, while it solves a similar problem to the Expression tree API, both are immutable but Roslyn does so in a quite different way: Expression nodes have no reference to the parent node, are modified using a ExpressionVisitor and that's why big parts can be reused. Roslyn's SyntaxNode , on the other side, has a reference to its parent, so all the nodes effectively become a block that's impossible to re-use. Methods like Update , ReplaceNode , etc, are

Building Expression Trees

穿精又带淫゛_ 提交于 2019-12-04 06:34:12
I'm struggling with the idea of how to build an expression tree for more lambdas such as the one below, let alone something that might have multiple statements. For example: Func<double?, byte[]> GetBytes = x => x.HasValue ? BitConverter.GetBytes(x.Value) : new byte[1] { 0xFF }; I would appreciate any thoughts. I would suggest reading through the list of methods on the Expression class , all of your options are listed there, and the Expression Trees Programming Guide . As for this particular instance: /* build our parameters */ var pX = Expression.Parameter(typeof(double?)); /* build the body