expression-trees

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

社会主义新天地 提交于 2019-12-07 02:59:44
问题 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

Linq to SQL throwing a StackOverflowException

懵懂的女人 提交于 2019-12-07 00:58:53
问题 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

Could not find a matching type in ExpressionSerialization.dll

家住魔仙堡 提交于 2019-12-06 15:26:43
I use ExpressionSerialization library for serializing Expression Tree but get me an error Could not find a matching type WCF Service : public class Person { public int ID { get; set; } public string Name { get; set; } } [ServiceContract] public interface IService1 { [OperationContract(Name = "GetByPredicate")] List<Person> Get(XElement expression); [OperationContract] List<Person> Get(); } public class Service1 : IService1 { private readonly List<Person> _persons; public Service1() { var p = new List<Person> { new Person() {ID = 1, Name = "A"}, new Person() {ID = 2, Name = "B"}, new Person()

NotSupportedException when using compiled lambda expression for Average

守給你的承諾、 提交于 2019-12-06 14:19:47
问题 I tried to answer this question but failed: So let's take the original query: var result = db.Employees.GroupBy(x => x.Region) .Select(g => new { Region = g.Key, Avg = g.Average(x => x.BaseSalary)}); Works fine. Now we want to dynamically decide what to average. I try to create the lambda for Average dynamically: string property = "BaseSalary"; var parameter = Expression.Parameter(typeof(Employee)); var propAccess = Expression.PropertyOrField(parameter, property); var expression = (Expression

Expression.Convert type for Expression.Property

 ̄綄美尐妖づ 提交于 2019-12-06 11:44:32
问题 I'm trying to convert a Parameter expression and having trouble with converting to value types. Below is a sample of my code: public static MemberExpression ConvertToType(ParameterExpression sourceParameter, PropertyInfo propertyInfo, TypeCode typeCode) { var sourceExpressionProperty = Expression.Property(sourceParameter, sourceProperty); //throws an exception if typeCode is a value type. Expression convertedSource = Expression.Convert(sourceExpressionProperty, Type.GetType("System." +

System.Linq.Dynamic.DynamicExpression parsing expressions with methods

十年热恋 提交于 2019-12-06 11:26:10
问题 I need to build a system where I have a number of expressions that are stored in a file. These expressions would be read into the program, compiled into linq expressions and evaluated as functions on a number of objects. However, these expressions would contain references to some functions in the code (i.e. they would not be made of just basic C# operators). I'm trying to use DynamicExpression from System.Linq.Dynamic and it almost works, except that my functions from the code are not

How to parameterize a selector with a function in EF query?

半腔热情 提交于 2019-12-06 11:07:36
问题 I have a projection function that I pass to IQueryable<>.Select() method: private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(){ return e => new PriceItem { Id = e.Id, Price = Math.Round(e.Price, 4) }; } Everything works just fine but I want to parameterize it like that: private static Expression<Func<VendorPrice, PriceItem>> GetPriceSelector(Func<VendorPrice, decimal> formula){ return e => new PriceItem { Id = e.Id, Price = formula(e) }; } so that I can call it like

Understanding Expression Tree and Parameter Evaluation

↘锁芯ラ 提交于 2019-12-06 09:23:37
问题 I'm attempting to modify an expression tree that dynamically builds a Contains expression that ultimately results in SQL like P IN (123, 124, 125, 200, 201) to instead check perform range checks, which ultimately results in SQL like (P >= 123 AND P <= 125) OR (P >= 200 AND P <= 201) I'm basing my solution on this post. static public Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>( Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values) { //

Compiled Expression Tree slow due to JIT_MethodAccessCheck

拈花ヽ惹草 提交于 2019-12-06 08:13:36
问题 We're using compiled Expression Trees to generate code dynamically; some information only available to us at runtime enables us to (in theory) write simpler, faster code. We do get a performance boost in many cases. However, in some cases we get a performance hit. In such cases, the Visual Studio Profiler shows that the difference in performance is due to this method (which doesn't show up at all in statically compiled code) JIT_MethodAccessCheck What does this method do? (Google doesn't have

Why compiled lambda build over Expression.Call is slightly slower than delegate that should do the same?

本秂侑毒 提交于 2019-12-06 07:55:08
问题 Why compiled lambda build over Expression.Call is slightly slower than delegate that should do the same? And how to avoid it? Explaining BenchmarkDotNet results. We are comparing CallBuildedReal vs CallLambda ; others two CallBuilded and CallLambdaConst are "subforms" of CallLambda and shows the equal numbers. But difference with CallBuildedReal is significal. //[Config(typeof(Config))] [RankColumn, MinColumn, MaxColumn, StdDevColumn, MedianColumn] [ClrJob , CoreJob] [HtmlExporter,