expression-trees

Linq to SQL throwing a StackOverflowException

萝らか妹 提交于 2019-12-05 06:23:17
I'm executing a pretty simple query using Linq to SQL. I'm creating the expression and then passing it to the Where() extension method. The Linq internals are throwing a StackOverflowException when I attempt to actually execute the query. Here is the code: int expectedCount = 4; Expression<Func<Thing, bool>> expression = ...; //Expression looks like (LocaleID = 1 && GenderID ==1 && (TimeFrameID == 2007 || TimeFrameID == 2008)) using (XYZDataContext context = new XYZDataContext()) { int count = context.Things.Where(expression).Count(); //... } And here is the DebugView of the expression:

Is there a generic method to iterate and print a values in an unknown collection?

一个人想着一个人 提交于 2019-12-05 06:02:07
Let's say, I have a Print method like this: private static void Print(IEnumerable items) { // Print logic here } I want to pass a collection class to this Print method, which should print all the fields like a table. For example, my input collection can be "Persons" or "Orders" or "Cars" etc. If I pass the "Cars" collection to the Print method, it should print the list of "Car" details such as: Make, Color, Price, Class etc. I won't know the type of the collection until run-time. I tried and achieved a solution using TypeDescriptors and PropertyDescriptorCollection . But, I don't feel that is

Binary expression tree C++

…衆ロ難τιáo~ 提交于 2019-12-05 04:23:30
问题 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

Expression.Lambda and query generation at runtime, nested property “Where” example

落爺英雄遲暮 提交于 2019-12-05 03:28:37
I found very nice answer on a question about building Expression Tree for Where query. Expression.Lambda and query generation at runtime, simplest "Where" example Can someone help me and show me how this example could be implemented in the scenario with nested property. I mean instead of: var result = query.Where(item => item.Name == "Soap") With that solution: var item = Expression.Parameter(typeof(Item), "item"); var prop = Expression.Property(item, "Name"); var soap = Expression.Constant("Soap"); var equal = Expression.Equal(prop, soap); var lambda = Expression.Lambda<Func<Item, bool>>

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

不羁岁月 提交于 2019-12-05 02:32:48
问题 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 :) 回答1: If you're

Linking a .NET Expression Tree into a new assembly

允我心安 提交于 2019-12-05 01:28:09
问题 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. The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the

How to convert CIL to a LINQ Expression Tree [closed]

南楼画角 提交于 2019-12-05 01:19:36
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Has any work been done on the direct conversion of CIL to LINQ expression trees? This would include class libraries, blogs, books, academic papers, etc. What are the known mismatches between CIL and LINQ

How Does Queryable.OfType Work?

拈花ヽ惹草 提交于 2019-12-05 00:51:34
问题 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( Expression.Call( null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof(TResult) }) , new Expression[] { source.Expression })); } So let me see if I've

Convert func to predicate using reflection in C#

耗尽温柔 提交于 2019-12-05 00:02:17
问题 I'm basically trying to do this, but I don't know what T will be, so I'm building things up using Reflection and Expression trees. // Input (I don't know about "Book") Type itemType = typeof(Book); // Actual Code // Build up func p => p.AuthorName == "Jon Skeet" ParameterExpression predParam = Expression.Parameter(itemType, "p"); Expression left = Expression.Field(predParam, itemType.GetField("AuthorName")); Expression right = Expression.Constant("Jon Skeet", typeof(string)); Expression

How to create an empty delegate using Expression Trees?

馋奶兔 提交于 2019-12-04 22:57:07
问题 Using anonymous methods you can create empty delegates since C# 2.0. public event EventHandler SomeEvent = delegate {}; public event Action OtherEvent = delegate {}; This is e.g. useful to prevent having to do the null check when invoking events. How can I create the same behavior using Expression Trees? The only possible option I see now is to use Expression.Lambda(), but as far as I can tell this would require a lot of extra work. 回答1: An expression tree, by nature of its purpose, always