expression-trees

Implement Not in expression trees, .net 4

僤鯓⒐⒋嵵緔 提交于 2020-01-13 11:01:36
问题 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

Evaluate C# expression inside another expression

試著忘記壹切 提交于 2020-01-13 07:58:07
问题 I want to use an expression in another one: Expression<Func<double, double>> f = x => x * x * 27 + blah ... expression with x; Expression<Func<double, double>> g = y => 3 + 8 * f.Compile()(y) * y * blah... expression with y and f(y); This will not work when sent to LINQ to SQL because f.Compile() is unknown to SQL. How do you evaluate the expression f on the variable y without compiling it, but still using normal syntax to define g ? I don't want to have to define all of g with some

Creating a function that converts functions of one type to another

半腔热情 提交于 2020-01-13 04:47:29
问题 For some fancy reflection stuff, I've got a function of type Func and need to pass it into a function that accepts type Func where T is not known until run time. For example: public bool MyOperation(Func<string,bool> op) { return _myValues.Any(op); } public static bool InvokeOperationMethod(MethodInfo info, object obj,Func<object,bool> opAsObject) { info.Invoke(obj, opAsObject); } The issue is that since I've got a lambda of a weaker type, I can't pass it as an argument of a stronger type. So

How can I transform this linq expression?

牧云@^-^@ 提交于 2020-01-13 03:40:08
问题 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

Generated methods for polynomial evaluation

有些话、适合烂在心里 提交于 2020-01-13 02:57:29
问题 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

Generated methods for polynomial evaluation

↘锁芯ラ 提交于 2020-01-13 02:57:24
问题 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

Generic Expression tree with 'OR' clause for each supplied property

此生再无相见时 提交于 2020-01-11 03:05:07
问题 I have created a generic search extension method for IQueryable that enables you to search for a single property to see if a search term is contained within it. http://jnye.co/Posts/6/c%23-generic-search-extension-method-for-iqueryable I now want to enable the user to select multiple properties to search within each, matching if any property contains the text. The code: The user enters the following code to perform this search: string searchTerm = "Essex"; context.Clubs.Search(searchTerm,

Lambda Expression to be used in Select() query

人走茶凉 提交于 2020-01-10 14:34:10
问题 I am trying to build a lambda expression, containing two assignments (as shown further down), that I can then pass to a Queryable.Select() method. I am trying to pass a string variable into a method and then use that variable to build up the lambda expression so that I can use it in a LINQ Select query. My reasoning behind it is that I have a SQL Server datasource with many column names, I am creating a charting application that will allow the user to select, say by typing in the column name,

How do I create a Linq expression tree with an F# lambda?

◇◆丶佛笑我妖孽 提交于 2020-01-09 10:02:43
问题 Here's what can be done in C# - var two = 2; System.Linq.Expressions.Expression<System.Func<int, int>> expr = x => x * two; expr.Compile().Invoke(4); // returns 8 I wish to do the precise equivalent in F#. Here's what I tried, but did not compile - let two = 2 let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>> expr.Compile().Invoke(4) // desired to return 8 Perhaps predictably, compilation fails on line 2 with the following error - "This function takes

Filter data with multiple complex clauses joined with OR operation in where function

眉间皱痕 提交于 2020-01-07 04:50:19
问题 How is it possible to filter data based on some set of filters or expressions that should be used with or operation in where clause? For example, there is a class: class DTOFilter { public string Domain { get; set; } public string Mobile { get; set; } } It is required to filter Users list based on the list of the filters next way: u=> (u.Email.Contains(filters[0].Domain) && u.PhoneNumber.StartsWith(filters[0].Mobile)) || (u.Email.Contains(filters[1].Domain) && u.PhoneNumber.StartsWith(filters