expression-trees

Detect parenthesis in BinaryExpression

假装没事ソ 提交于 2019-12-12 08:09:07
问题 I am building a expression analyser from which I would like to generate database query code, I've gotten quite far but am stuck parsing BinaryExpressions accurately. It's quite easy to break them up into Left and Right but I need to detect parenthesis and generate my code accordingly and I cannot see how to do this. An example [please ignore the flawed logic :)]: a => a.Line2 != "1" && (a.Line2 == "a" || a.Line2 != "b") && !a.Line1.EndsWith("a") I need to detect the 'set' in the middle and

How to get Property Value from MemberExpression without .Compile()?

谁都会走 提交于 2019-12-12 08:04:26
问题 I'm having issues trying to get the value of an object out of the Expression Tree without using .Compile() The object is quite simple. var userModel = new UserModel { Email = "John@Doe.com"}; The method giving me issues looks like this. private void VisitMemberAccess(MemberExpression expression, MemberExpression left) { var key = left != null ? left.Member.Name : expression.Member.Name; if (expression.Expression.NodeType.ToString() == "Parameter") { // add the string key _strings.Add(string

How do i convert this Func<SampleExpression,IEnumerator<string>,bool>> to Func<SampleExpression,bool>>

蹲街弑〆低调 提交于 2019-12-12 03:16:23
问题 This is my class class SampleExpression { public string str; public static bool SampleEnum(SampleExpression s, IEnumerator<string> ien = null) { while (ien.MoveNext()) { if (s.str == ien.Current) { ien.Reset(); return true; } } return false; } } This is how i am generating my expression tree at runtime: static void Main(string[] args) { ParameterExpression param1 = Expression.Parameter(typeof(SampleExpression), "token"); ParameterExpression param2 = Expression.Parameter(typeof(IEnumerator

Where clause not working with parantheses

▼魔方 西西 提交于 2019-12-12 02:27:09
问题 Suppose the following Query using a NH 3.4 and RepositoryPattern var list = _repository .QueryOver() .Where(x => (x.Age > 20)) // notice the parantheses .Future() .ToList(); Whith these parantheses added the NH is failing to work, and causes a SO exception. If replacing .Where(x => (x.Age > 20)) with .Where(x => x.Age > 20) it works as expected. Any clues on why it doesn't work with extra parantheses? Note This is a simplified scenario from the bigger picture. In production i'm passing that

Converting a Postfix Notation to an ExpressionTree

二次信任 提交于 2019-12-12 01:48:21
问题 As it is said in the title I am trying to create a code which converts a postfix notation to an expression tree. Here you can check the constructor : public byte type; // 0 : operator, 1: operand (a number) public char operator; // One of '+', '-', '*', '/' public int operand; // A number ExpressionTreeNode(byte type){this.type = type; left=right=null;} and Here is my code : public static ExpressionTreeNode Postfix2ExpressionTree(String postfixExpr){ Stack s = new Stack<Object>();

Will manual Linq-To-Sql mapping with Expressions work?

雨燕双飞 提交于 2019-12-12 00:57:27
问题 I have this problem: The Vehicle type derives from the EntityObject type which has the property "ID". I think i get why L2S can't translate this into SQL- it does not know that the WHERE clause should include WHERE VehicleId == value . VehicleId btw is the PK on the table, whereas the property in the object model, as above, is "ID". Can I even win on this with an Expression tree? Because it seems easy enough to create an Expression to pass to the SingleOrDefault method but will L2S still fail

Generic method to calcuate Euclidean Distance using expressions

大城市里の小女人 提交于 2019-12-12 00:27:57
问题 I'd like a method that can calculate the Euclidean distance using expressions and order an IQueryable: sqrt[(q1 - p1)^2 + (q2 - p2)^2 + ... + (qn - pn)^2] This is the method signature I've come up with: public static IOrderedQueryable<T> EuclideanDistanceOrder<T>( this IQueryable<T> query, IEnumerable<Expression<Func<T, double>>> expressions) { var orderedQuery = query.OrderBy(i => Math.Sqrt(expressions.Aggregate((total, item) => total + Math.Pow(item, 2)))); return orderedQuery; } I'm not

Create Expression for List.Any clause

此生再无相见时 提交于 2019-12-12 00:12:13
问题 Consider this hierarchy of classes. class Event { public Attendees[] AttendeesList; } class Attendees { public ComplexProperty Property; public object Value; } class ComplexProperty { } class Program { static void Main(string[] args) { // There are constants. ComplexProperty constproperty = new ComplexProperty(); object constValue = 5; // consider this linq query: Event evnt = new Event(); var result = evnt.AttendeesList.Any((attnds) => attnds.Property == constproperty && attnds.Value ==

Expression to convert IQueryable<t> int List<SelectListItem>

女生的网名这么多〃 提交于 2019-12-11 20:14:18
问题 I would like to create a repository method like this: public List<SelectListItem> AllAsSelectListItems( Expression<Func<T, string>> valueProperty, Expression<Func<T, string>> textProperty, string selectedValue = "") { // what goes here? I am having serious trouble with this bit! } That will enable me to call it like this: List<SelectListItem> selectListItems = PersonRepository.AllAsSelectListItems( m => m.ID, m => m.Name, selectedIDAsString ); And, with the selectedValue parameter being "1",

Building expression tree

核能气质少年 提交于 2019-12-11 13:25:25
问题 Given a expression like 4 * 5 + 9, how can we build a expression tree out of this? I was reading this interview question on job portal and thought of trying it. The problem is if this had been parenthesized, it would had been little easier to build For example ((4 * 5) + 9). Then with the opening of left parenthesis, we would know we have to go left, wherein the number would be a leaf node and operator would be parent, and once we hit right parenthesis, we would return and go up the level.