expression-trees

How do I view an expression tree with LINQPad?

Maybe I'm missing something obvious, but I how can I view the expression tree for this query: from word in "The quick brown fox jumps over the lazy dog".Split() orderby word.Length select word using LINQPad? ...

Is this is an ExpressionTrees bug?

using System; using System.Linq.Expressions; class Program { static void Main() { Expression<Func<float, uint>> expr = x => (uint) x; Func<float,uint> converter1 = expr.Compile(); Func<float,uint> converter2 = x => (uint) x; var aa = converter1(float.MaxValue); // == 2147483648 var bb = converter2(float.MaxValu...

How to select/filter against substring in a list of strings?

I have a LINQ result set I'm trying to filter in a strange and peculiar way. List<string> MyDomains = [Code to get list]; var x = (from a in dc.Activities where a.Referrer != null && a.Referrer.Trim().Length > 0 && !a.Referrer.Contains("localhost") && a.SearchResults.Count() == 0 orderby a.ID descendin...

Can't get operator overloading to work with Linq Expression Trees

I am creating Linq expression trees from F# that operates on a custom datatype I have. The type is a very simple discriminated union that has the usual arithmetic operators overloaded. But for some reason I cannot create arithmetic linq expression nodes due to the fact that it can't find the correct overload. Thing is, I swear I had this...

Is this is an ExpressionTrees bug? #2

Looks like ExpressionTrees compiler should be near with the C# spec in many behaviors, but unlike C# there is no support for conversion from decimal to any enum-type: using System; using System.Linq.Expressions; class Program { static void Main() { Func<decimal, ConsoleColor> converter1 = x => (ConsoleColor) x; ConsoleColor...

Is there a library to get/set property from a string (xpath or Property Path)

I am looking for a way to use a string instance path or xpath expression to get/set property values very similar to what wpf does with it's data binding. Do you know of a library for this? I found ObjectXpathNavigator but I am not sure if there is something a little more active out there. Get("Person.FirstName"); Set( "Person/LastName...

Need help building LINQ to SQL Expression

I need to translate the following Code to an Expression and I will explain why: results = results.Where(answer => answer.Question.Wording.Contains(term)); results is IQueryable<ISurveyAnswer> Question is ISurveyQuestion Wording is String The problem is, Question is not always the name of the LINQ to SQL property. This will give me t...

Is this is an ExpressionTrees bug? #3

Expressions class should be more accurate while searching for user-defined operators? sealed class Foo { // just the private static method! private static int op_Implicit() { return 1; } public static implicit operator int(Foo foo) { return 2; } } public class Program { private static void Main() { var param = Expression...

Is this is an ExpressionTrees bug? #4

This is not "close to real" situation, but it shows that Expressions API doesn't look for operators in the destination type of conversion if it founds the suitable operator in the source type. sealed class Foo { public static explicit operator Bar(Foo foo) { return null; } } sealed class Bar { public static implicit operator Bar(Fo...

C#: How to test that an expression is short-circuited

I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second) { ... } I have written a test-case for it that makes sure the two expressions are in fact combined. At least so that the new expression I get works. Now I ...

Negating a method call in an Expression tree

I'm generating a c# Linq expression dynamically as below, which will (in the example below) run string.Contains against the collection values. var dynamicMethod = "Contains"; var parameter = Expression.Parameter(typeof (MyClass), "type"); var property = Expression.Property(parameter, "MyProperty"); var constantValue = Expression.Constan...

Need guidance towards evaluative boolean logic tree

I can't seem to find a pointer in the right direction, I am not even sure what the terms are that I should be researching but countless hours of googling seem to be spinning me in circles, so hopefully the collective hive of intelligence of Stack Overflow can help. The problem is this, I need a way to filter data in what I can only call...

Using an expression tree to read the name and value of a property. Is there an alternative?

Assumptions Suppose I have a class with a property: class ClassWithProperty { public string Prop { get; private set; } public ClassWithProperty(string prop) { this.Prop = prop; } } And now suppose I have created an instance of that class: var test = new ClassWithProperty("test value"); What I want I want ...

How to wrap Entity Framework to intercept the LINQ expression just before execution?

I want to rewrite certain parts of the LINQ expression just before execution. And I'm having problems injecting my rewriter in the correct place (at all actually). Looking at the Entity Framework source (in reflector) it in the end comes down to the IQueryProvider.Execute which in EF is coupled to the expression by the ObjectContext off...

Combining multiple expressions (Expression<Func<T,bool>>) not working with variables. Why?

ok guys, bare with me. I'll summarize first, then go into detail. I've written a number of methods (.WhereOr, .WhereAnd) which basically allow me to "stack up" a bunch of lambda queries, and then apply them to a collection. For example, the usage with datasets would be a little like this (although it works with any class by using generi...

Expression trees for code which doesn't type check

Is it possible to construct an expression tree in .NET 3.5 from code like Expression expr = () => (true + new object()) ? It seems to be impossible, since "A compiler-generated expression tree is always rooted in a node of type Expression", but I may be missing something. Obviously, such a tree can be constructed manually. ...

Getting the result from an Expression

I've created a lambda expression at runtime, and want to evaluate it - how do I do that? I just want to run the expression by itself, not against any collection or other values. At this stage, once it's created, I can see that it is of type Expression<Func<bool>>, with a value of {() => "MyValue".StartsWith("MyV")}. I thought at that ...

Optimal representation of expressions in F#

I'm working on a library to generate SQL from LINQ expressions (basically a modified subset of LINQ-to-SQL). I'm using discriminated unions to model the SQL expressions, but have encountered some (perceived?) limitations. I want to do something like the following (note the last line): type SqlSourceExpression = | Table of string ...

Binary Operator Equal is not defined... troubles comparing MemberExpressions in a generic repository

I have a generic SelectAllByKey method in my repository: public IList<E> SelectAllByKey(string columnName, string key) { KeyProperty = columnName; Expression rightExpr = null; rightExpr = Expression.Constant(key); // First we define the parameter that we are going to use the clause. var ...

Construct a Linq Expression from a generic method.

(sorry, I tried to format it well, but I cannot get the code formatting to work correctly) I get: Incorrect number of arguments supplied for call to method 'System.Linq.IQueryable`1[System.String] Take[String](System.Linq.IQueryable`1[System.String], Int32)' when I execute: string[] companies = { "Consolidated Messenger", "Alpine Sk...