expression-trees

How Does Queryable.OfType Work?

Important The question is not "What does Queryable.OfType do, it's "how does the code I see there accomplish that?" Reflecting on Queryable.OfType, I see (after some cleanup): public static IQueryable<TResult> OfType<TResult>(this IQueryable source) { return (IQueryable<TResult>)source.Provider.CreateQuery( ...

Linq2SQL "or/and" operators (ANDed / ORed conditions)

Let's say we need to apply several conditions to select from a table called "Things" (unknown count and nature) if conditions are known, we can write db.Things.Where(t=>foo1 && foo2 || foo3); but if we have to build that Where condition programatically, I can imagine how can we apply ANDed conditions IQuerable DesiredThings = db.Thi...

How to preserve Linq2SQL OR between conditions?

Lets say we need to select two sets from a table: "Things" var GradeA = db.Things.Where(t=> condition1); var GradeB = db.Things.Where(t=> !condition1 && condition2); var DesiredList = GradeA.union(GradeB); alternatively, we need to write a single statement to avoid union cost: var DesiredList = db.Things.Where(t=> condtion1 || (!con...

Solution for Linq2SQL expressions that has no translation

Is there a way to provide translation for expressions that have no translation ? like double.parse() ...

Java Expression Trees

Is there an equivalent of .net's Expression Trees that underly LINQ for the JVM? I would like to implement some LINQ like code structures in Scala and I am wondering if I have to roll my own expression tree library also. Update: I am not interested in a linq equivalent itself. .net has a large set of expression tree tools that make it e...

Is there any way to programmatically generate Python bytecode?

I want to hack around with the Python interpreter and try creating a small DSL . Is there any module where I can do something like this theoretical code (similar to LINQ expression trees)? expression_tree = Function( Print( String('Hello world!') ) ) compile_to_bytecode(expression_tree) Or would it just be easier t...

C#, Linq to Sql: Why can I not use an expression to filter SubEntities?

I have made myself an ExpressionBuilder class that helps me put together expressions that can be used as a predicate when doing Linq to Sql queries. It has worked great. However, I just discovered Expressions can only be used to filter on Tables, and not on EntitySets??Why on earth is this the case? For example if I have Company and an ...

Linq to EF Expression Tree / Predicate int.Parse workaround

Hi All, I have a linq Entity called Enquiry, which has a property: string DateSubmitted. I'm writing an app where I need to return IQueryable for Enquiry that have a DateSubmitted within a particular date range. Ideally I'd like to write something like IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>(); int dateSt...

Whats a good use case for .net 4.0 Expression Trees?

This one was inspired by my language-guru co-worker who can't seem to find a good use for them, and after a few lame attempts of my own, I'd have to agree. Now I know these concepts tend to flow a lot more easily once you get some good practical reasons down. At the moment it seems as though its only purpose is to allow you to write...

Base a small expression DSL on the DLR or keep it hand-rolled in F#?

I'm building a spreadsheet-like application, where a lot of small calculations needs to be stitched together in a tree-structure. These calculations are user-defined and I need a way for the user to enter them at runtime. My current approach is to write a small "expression DSL" in F#, where I parse the input with FParsec, build a syntax...

Calling a F# function via a Linq expression tree MethodCallExpression node?

Hello, I am trying to create an expression tree containing a function call to a F# function on a certain module. However, I am missing something because the System.Linq.Expressions.Expression.Call() helper function cant find the function I'm supplying. The Call() call gives an InvalidOperationException: "No method 'myFunction' on type ...

Lambda Parameter not in scope -- while building binary lambda expression

When creating a lambda expression by hand I get a 'Parameter not in scope' exception. Any ideas as to what I am doing wrong? public class OtherType { public string First_Name { get; set; } public string Last_Name { get; set; } } static void Main(string[] args) { Expression<Func<OtherTy...

Bit Curious to understand Expression Tree in .NET

I have read several articles and several stackoverflow.com posts about expression tree. It is beating my brain to understand. Questions: 1) Like DOM (Document Object Model), it is an in-memory representation of logic? 2) Somebody explained it is a mechanism to translate an executable code into data, using it we can produce a data str...

Refactoring Func<T> into Expression<Func<T>>

I have a method that currently takes a Func<Product, string> as a parameter, but I need it to be an Expression<Func<Product, string>>. Using AdventureWorks, here's an example of what I'd like to do using the Func. private static void DoSomethingWithFunc(Func<Product, string> myFunc) { using (AdventureWorksDataContext db = new Adven...

Retrieving an Expression from a property and adding it to an expression tree

I've tried to simplify this example, as the actual code I'm playing with is more complex. So while this example may seem silly, bear with me. Let's say I'm working with the AdventureWorks database and I decide I want to add a property called Blarg to the Product table that returns an expression that contains code I would like to use in...

Expression Tree to Infix: Null in one instance, but not in another?

I have an expression tree class, which has a method buildExpressionTreeFromQueue. As its name suggests, the method takes a queue (just a series of operators and operands) and cycles through it, adding nodes to the tree appropriately. My problem is this: when I try to write the tree as Infix notation (or Prefix or Postfix for that matter...

Getting the object out of a MemberExpression?

So, lets say I have the following expression in C#: Expression<Func<string>> expr = () => foo.Bar; How do I pull out a reference to foo? ...

Linking a .NET Expression Tree into a new assembly

I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies. ...

Avoiding unnecessary boxing in DLR

I'm playing with DLR to get a better understanding of it. I'm not completely familiar yet with all its concepts and its terminology so sorry for any terminological or conceptual mistakes in my question. Basically, the way I understand it is that you pass around objects in expression trees but you use binders in order to expose your obj...

Generated methods for polynomial evaluation

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 nth 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 sh...