expression-trees

Bit Curious to understand Expression Tree in .NET

会有一股神秘感。 提交于 2019-12-04 05:47:20
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 structure representing the code. Does it mean, expression trees are used to design a user defined pattern? 3) Most of the Examples show Expression tree in conjunction with Func<> delegate or other delegates, So using delegate and other programming construct can’t we

LINQ Expression Tree Any() inside Where()

偶尔善良 提交于 2019-12-04 04:36:15
I'm trying to generate the following LINQ query: //Query the database for all AdAccountAlerts that haven't had notifications sent out //Then get the entity (AdAccount) the alert pertains to, and find all accounts that //are subscribing to alerts on that entity. var x = dataContext.Alerts.Where(a => a.NotificationsSent == null) .OfType<AdAccountAlert>() .ToList() .GroupJoin(dataContext.AlertSubscriptions, a => new Tuple<int, string>(a.AdAccountId, typeof(AdAccount).Name), s => new Tuple<int, string>(s.EntityId, s.EntityType), (Alert, Subscribers) => new Tuple<AdAccountAlert, IEnumerable

How to write string.Contains(someText) in expression Tree

ⅰ亾dé卋堺 提交于 2019-12-04 04:26:58
问题 This is the tutorial I'm following to learn Expression Tree. I've more than 35 columns to display, but the user can chose to display 10 columns at once. So one the user type something in the search box, I want to search only the columns that are visible to the user. SELECT FirstName, LastName, Address, ..., State FROM Students WHERE Id == @Id col1 AND ( FirstName LIKE '%@searchText%' OR LastName LIKE '%@searchText%' OR Address LIKE '%@searchText%' OR ... State LIKE '%@searchText%') Back to

Create fully dynamic where clause with expression tree and execute on IQueryable

我与影子孤独终老i 提交于 2019-12-04 04:26:01
At point (3) in my code I have defined a query called query1 in which I defined a .Where lambda expression. This query is in some way dynamic but still contains static elements, it always refers to the Type Employee and its (int) property ClientID. Now I very much like to make the refering to the type and its property dynamic, based on the method parameters which by example are shown below point (1). What I tried to so far is making the static part of the query defined under point (3) fully dynamic by replacing it with a more elaborate expression tree as written down in (4), (5) & (6). But

Create Func to return both reference- and value-types

断了今生、忘了曾经 提交于 2019-12-04 03:43:43
问题 I have a method returning a Func<object> built by an expression as follows: var expr = Expression.Property( Expressions.Expression.Constant(new Foo { Name = "Hans", Age = 3 }, typeof(Foo)), "Age"); var f = Expression.Lambda<Func<object>>(expr).Compile(); This expression should return the Age -property of this dummy Foo -object. The problem is that as I want to return a Func<object> instead of a Func<int> I get an ArgumentException: An expression of type System.Int32 cannot be used as return

How to assign a value via Expression?

一笑奈何 提交于 2019-12-04 03:16:32
This would be very simple if I were able to assign via a Lambda expression (below) //An expression tree cannot contain an assignment operator Expression<Func<ComplexObj, object>> expression = obj => obj.Contacts[0].FirstName = "Tim"; This code above is invalid due to the assignment operator. I need to pass a lambda expression in order to identify the property in the complex object that needs to be set. In some cases the complex object has List and therefor duplicate object types and names which is why I need the lambda to explicitly reference the field in the object to be updated. I am able to

Lambda Parameter not in scope — while building binary lambda expression

a 夏天 提交于 2019-12-04 01:11:49
问题 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<OtherType, bool>> l2 = p => p.First_Name == "Bob"; l2.Compile(); // Works PropertyInfo property = typeof(OtherType).GetProperty("First_Name"); ParameterExpression para = Expression.Parameter(typeof(OtherType), "para");

How to reflect over T to build an expression tree for a query?

蓝咒 提交于 2019-12-03 22:45:27
I'm trying to build a generic class to work with entities from EF. This class talks to repositories, but it's this class that creates the expressions sent to the repositories. Anyway, I'm just trying to implement one virtual method that will act as a base for common querying. Specifically, it will accept a an int and it only needs to perform a query over the primary key of the entity in question. I've been screwing around with it and I've built a reflection which may or may not work. I say that because I get a NotSupportedException with a message of LINQ to Entities does not recognize the

How to include an And() Expression that checks for a Property and it's value

旧时模样 提交于 2019-12-03 21:34:29
I'd like to add a check in our repository that filters all objects out on a companyId if it's there and if it matches a given value. So where we have: public T First<T>(Expression<Func<T, bool>> expression) where T : EntityObject { var set = GetObjectSet<T>(); return set.FirstOrDefault<T>(); } I would like to add line which looks something where... express.And("Check for CompanyId property if it exists then make sure it = 3"); Any ideas on how to go about this? Thanks :) If you're looking for a function which you can use to tack on a company id check to your expression if a company id exists

Binary expression tree C++

ぃ、小莉子 提交于 2019-12-03 20:27:27
I have a little problem. I'm trying to add a mathematical expression to a binary tree but I can't understand the algorithm. Here it is: If the current token is a '(': Add a new node as the left child of the current node, and descend to the left child. If the current token is in the list ['+','-','/','*']: Set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child. If the current token is a number: Set the root value of the current node to the number and return to the parent. If