Questions tagged [expression-trees]

1

votes
1

answer
93

Views

EF Core dynamic filter

I've been working in a Dynamic filter class for EF Core queries using Expression trees, everything looks good, filter is working, I can pass a filter collection and it works, but when I look at the SQL sentence, it is querying the whole table and applying the filter on the resulting collection, here...
Mauricio Atanache
1

votes
0

answer
49

Views

Express Trees: Rewrite query predicates to use joins

I want to intercept a query to a database sent via Entity Framework and convert from something like SELECT * FROM EntityA where (EntityA.Title Like 'foo' OR EntityA.Description Like 'foo') and EntityA.Prop1 in (1,2,3) to SELECT * FROM EntityA INNER JOIN EntityB on EntityA.ID = EntityB.ID WHERE (E...
rism
1

votes
0

answer
24

Views

Expression Trees

I'm currently writing a class that reads lines from a txt file and sorts then using Reverse Polish Notation (RPN). I've started writing the method 'printExpressionTree' that will correctly format the data from the txt file. My code so far: static void printExpressionTree(TreeNode tmp) { if(tmp.isL...
1

votes
0

answer
48

Views

Create Expression tree that defines only lambda body

As stated by the title - I want to define an expression, which describes only the body of a method. Example: // Definition public Expression SomeExpression() { ... } // Usage entities.Select(e => e.SomeExpression()) // Instead of entities.Select(SomeExpression()) I know I can define a delegate Expr...
Alex
1

votes
0

answer
60

Views

Using DbFunctions method with anonymous type New Expression

I need to build an expression: { t => new { field1 = t.field1, field2 = t.field2, dateTimeField = TruncateTime(t.dateTimeField) } } I have built expression: { t => new { field1 = t.field1, field2 = t.field2, dateTimeField = t.dateTimeField } } by using LatticeUtilsAnonymousTypes, but I cannot wrap...
Andrey Dengin
1

votes
0

answer
60

Views

Equivalent of Delegate.Target for LambdaExpression

Given the following class: public class Foo { public string Bar {get; set;} Func GetFunc = () => Bar; } the following code will print True: var foo = new Foo(); var fn = foo.GetFunc; Console.WriteLine(fn.Target == foo); because the target of the action delegate is the instance of the object. How can...
Zev Spitz
1

votes
1

answer
52

Views

Dynamic GroupBy LINQ [duplicate]

This question already has an answer here: Build GroupBy expression tree with multiple fields 2 answers Suppose I have a class public class Item { public int Field1{get;set;} …… public int FieldN{get;set;} } And I want to use group by like this; Collection.GroupBy(selector“Field1,…,FieldK...
mahul17
1

votes
1

answer
41

Views

Expression Tree Group and Select using Column with any Type

I'm using a variation of the code from the excellent thread below to dynamically build a treeview structure from any given data source using a custom list of columns. Expression.Call GroupBy then Select and Count()? My code is a recursive variation of the code presented in this solution: public stat...
DaveW
1

votes
1

answer
49

Views

How to use one C# expression inside another C# expression for Entity Framework?

Suppose that I have some C# code that looks like this: var query1 = query.Where(x => x.BirthDate > now); var query2 = query.Where(x => x.EnrollmentDate > now); var query3 = query.Where(x => x.GraduationDate > now); The actual code is more complicated, but I'm using a simple example. I'm passing thi...
1

votes
0

answer
34

Views

How to reconstruct linq expression tree

How to inspect linq expression tree, so it could be rebuilt statement by statement (using System.Linq.Expressions.Expression methods)? I'm using DebugView from VS17 to visualise expression, but it's not very user friendly to read. Maybe there are better options?
user2820173
1

votes
1

answer
22

Views

How to create expression for ordering string by length

I am new to expression tree. I need to order my collection base on string length and then string. I dont want to repeat my code for every entity. Any body know how to write it with expression tree? somthing like this: public static IQueryable OrderByPropertyOrField(this IQueryable queryable, string...
mahmood
1

votes
1

answer
310

Views

What is the best way to ReadLine by Expression Tree?

If I want to get a user input from Console to my Expression Tree. What is the best way to do it? and how to make variable 'name' duck typing? Here are my code. using System; using System.Reflection; using System.Collections.Generic; using Microsoft.Linq; using Microsoft.Linq.Expressions; namespace...
1

votes
1

answer
565

Views

Patterns or techniques for designing a flexible advanced search with Linq Expression Trees

I'm looking to add an 'advanced search' capability to my ASP.NET/SQL Server 2005 application. Ideally, I'd like it to be table driven. For example, if my schema changes with the addition of a new column to a table that I want to search, I'd like to have the UI reflect the addition of the new colum...
Howard Pinsley
1

votes
1

answer
1.9k

Views

C# Expression Tree Parameter for Parent Navigation Property

How can I create a ParameterExpression for the parent side of a 1 to * Navigation Property? The following works for the child entity: var parameter = Expression.Parameter( typeof(T), // where T is the entity type GetParameterName()); // helper method to get alias Trying something similar on TParent...
blu
1

votes
1

answer
350

Views

Generic Repository Linq2Sql impedence mismatch problem

I am working on a repository pattern where the API look as follows: var visitor = repository.Find(x => x.EmailAddress == credentials.EmailAddress && x.Password == credentials.Password); where visitor is a domain object and x represents this domain object. The method signature of the Find method on...
Michael Mann
1

votes
2

answer
1.1k

Views

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 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 descending s...
tsilb
1

votes
1

answer
109

Views

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...
Alexey Romanov
1

votes
1

answer
211

Views

How do I subscribe to an event of an object inside an expression tree?

Sorry I couldn't think of a better title. This is a two part question that only make sense together. Say I have a constructor like this public Fact(INotifyPropertyChanged observable, Func predicate) { this.predicate = predicate; observable.PropertyChanged += (sender, args) => PropertyChanged(this, n...
firefly
1

votes
1

answer
411

Views

parsing expression trees with booleans

I am trying to parse an expression tree for a linq provider and running into a little snag with booleans. I can parse this no problems. var p = products.Where(x=>x.IsAvailable == true).ToList(); however when its written like this? var p = products.Where(x=>x.IsAvailable).ToList(); i only get a Membe...
Schotime
1

votes
2

answer
1.4k

Views

Evaluating an expression stored as a string

I want to store a boolean expression in a database, and evaluate it. It’s not necessary to store the complete expression tree, a string probably do it. I imagined a scheme like this: Criteria (expression_to_evaluate, value_to_return) For example, let’s say that I have an expression that matches...
Tommy Jakobsen
1

votes
3

answer
3.9k

Views

How do I dynamically construct a predicate method from an expression tree?

Here's the scenario: Silverlight 4.0, DataGrid, PagedCollectionView itemssource. The objective is to apply a Filter to the PCV. The filter needs to be a Predicate(Method) - where Method implements some logic against the object and returns true/false for inclusion. What I have is a need to optionall...
tobewan
1

votes
1

answer
396

Views

Linq: Dynamic Query Contruction: query moves to client-side

I've been following with great interest the converstaion here: Construct Query with Linq rather than SQL strings with regards to constructing expression trees where even the table name is dynamic. Toward that end, I've created a Extension method, addWhere, that looks like: static public IQueryable a...
donundeen
1

votes
1

answer
752

Views

Expression.Condition(nullableType.HasValue, new classInstance(){ … }, null) can it be done some other way?

I am working on a projection utility and have one last (more?) hurdle to clear... Here is the scenario: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int? AddressID { get; set; } public Address Address { get; set; } public string Otherpropert...
Mike
1

votes
1

answer
153

Views

.NET Expression Tree / Lambda Lifting framework?

Anybody know of a nice package that allows implicit editing of Expression Trees? Since you can't really edit them, just returning a new tree would be fine. Easy methods for Lambda lifting (or 'funcletization') would be a nice bonus. I remember a while back I was looking into this and I thought I fou...
Shlomo
1

votes
1

answer
593

Views

linq: what is the expression tree syntax for cross join

how to write this linq query in expression tree syntax from x in 100.To(999) from y in 100.To(999) let product = x * y where product.IsEven() select product
Rony
1

votes
1

answer
359

Views

How to make generic function that return int or date or string

I need to realize a function which return an Expression, where T could be an integer, a string or a datetime. The caller won't know the type of T. What I want to achieve is a function which create a predicate that can be used in a Linq OrderBy function. The should be something like: class FilterCrea...
themarcuz
1

votes
1

answer
466

Views

Assign a value from an expression mapping

I map properties using Expression. I've just needed to fetch property values from objects until know. How can I assign values using that expression?
jgauffin
1

votes
2

answer
1.1k

Views

How can I expand(inline) property in an expression(C# 4.0)?

I have an expression of this form: Expression expr = z => z.Show.OrgName == 'xyz'; I need to convert/expand it to following form: Expression expr = z => z.Show.Organization.Name == 'xyz'; where OrgName property on Show entity resolves to Organization.Name. How can I achieve this assuming I need this...
Anand
1

votes
2

answer
612

Views

Calling Compile() on Expression<T> multiple times and caching

I haven't found any information on MSDN regarding this problem. If we create an Expression and call Compile() on it does CLR cache compiled results, so any subsequent calls of Compile() on the same expression wouldn't have much impact because CLR would return previously compiled Func from internal m...
Robert Koritnik
1

votes
1

answer
73

Views

Is there an equilivent to Linq.Expressions.Expression in python?

I would like to be able to get the expression out of a lambda function much like C# does and parse it into something else? Example in C#: void Foo(Expression expression { // ... } Foo(someObj => someObj.HasBar); The lambda operator will be traslated to an expression that could be inspected. What's...
the_drow
1

votes
1

answer
186

Views

How can I factor out clauses in a parameterized Expression?

Hey all. I'm trying to optimize a Linq to Entities call by statically caching and reusing a compiled query. The query checks the same thing for a variable number of filter arguments, and the only way to compile query arguments like that is to explicitly use a number of arguments (rather than some...
dks1983
1

votes
2

answer
662

Views

Dynamically construct Select clause of a Linq query

I'm using a LINQ to Entities, and I have a couple of queries for which I want to be able to specify the Select clause at runtime. I figured I'd have to do it by building an Expression and adding it to the IQueryable, but I'm not sure how to do this. Can anybody give me a hint?
Rik
1

votes
2

answer
1.7k

Views

Is there a way to wrap Html.CheckboxFor, Html.TextboxFor, etc. methods with custom logic?

I am working with legacy models that wrap data and meta-data up in a single property. For the purpose of this question, suppose the interface is: pubic interface ILegacyCheckbox { bool Value { get; set; } bool Editable { get; set; } } I want to wrap the CheckBoxFor() extension method with my own,...
afeygin
1

votes
1

answer
569

Views

Combine some lambda expressions using Or operand

I want to make a dynamic expression which will be generated out of an unknown number of Or conditions: IQueryable entities = Repository.Load(); Expression wholeFilter; foreach(condition in Conditions) { Expression filter = e => e.something1 == condition.First && e.something2 == condition....
Javid_p84
1

votes
1

answer
234

Views

C# Expression Tree's always wrapping with ()

I'm trying to create an Expression tree that will determine if a line in a word document is deleted or not. I'm looping through a collection of dependencies where I have a comparisonExpression: comparisonExpression = Expression.Equal(Expression.Constant(attribute.AttributeValue), Expression.Constant...
Ryan
1

votes
1

answer
160

Views

Use of custom expression in LINQ leads to a query for each use

I have the following problem: In our database we record helpdesk tickets and we book hours under tickets. Between those is a visit report. So it is: ticket => visitreport => hours. Hours have a certain 'kind' which is not determined by a type indicator in the hour record, but compiled by checking va...
Paul77
1

votes
2

answer
763

Views

Remove part of Expression

I have a generic method: public void ExpressionBuilder() where T: IEntity { Expression expr = e => e.Id; //Id is part of IEntity ... } The resulted expression contains casting of T to IEntity: e => Convert(e).Id I want to remove this casting (to make it possible for EF to generate SQL). In other wor...
SiberianGuy
1

votes
1

answer
526

Views

lambda expression in boo

dose boo understand Expression tree? I try to compile this line with sharp develop exp as System.Linq.Expressions.Expression[of Func[of SomeClass, bool]] = { p as Text | return (p.Name == 'tttt') } but sharp develop raised this error Cannot convert 'callable(testlinq.SomeClass) as bool' to 'System....
1

votes
1

answer
125

Views

Different ways to parse / visit Expressions when traversing

I have a basic question about parsing expression trees. Is there a difference between using if statements to determine the NodeType of an expression, and handling it accordingly, versus dispatching the expression do a different Visitor method? Also is this even the correct way to dispatch: protecte...
Sean Thoman
1

votes
1

answer
920

Views

using reflection and linq for dynamic linq

I wrote this code: string Dcname = 'DataClasses1DataContext'; string TableName = 'Order'; var dc =(DataContext)Activator.CreateInstance(typeof(DataContext), Dcname); ITable mytbl=(ITable)dc.GetType().GetProperty(TableName).GetValue(dc,null); I want to query mytable using linq or Expression Tree. How...
Arian

View additional questions