expression-trees

Is this is an ExpressionTrees bug? #2

…衆ロ難τιáo~ 提交于 2019-12-04 18:17:58
问题 Looks like ExpressionTrees compiler should be near with the C# spec in many behaviors, but unlike C# there is no support for conversion from decimal to any enum-type : using System; using System.Linq.Expressions; class Program { static void Main() { Func<decimal, ConsoleColor> converter1 = x => (ConsoleColor) x; ConsoleColor c1 = converter1(7m); // fine Expression<Func<decimal, ConsoleColor>> expr = x => (ConsoleColor) x; // System.InvalidOperationException was unhandled // No coercion

C#: An item with the same key has already been added, when compiling expression

江枫思渺然 提交于 2019-12-04 17:04:29
问题 Ok, here's a tricky one. Hopefully there is an expression guru here who can spot what I am doing wrong here, cause I am just not getting it. I am building up expressions that I use to filter queries. To ease that process I have a couple of Expression<Func<T, bool>> extension methods that makes my code cleaner and so far they have been working nicely. I have written tests for all of them except one, which I wrote one for today. And that test fails completely with an ArgumentException with a

How do I specify the object to return from an expression tree method?

和自甴很熟 提交于 2019-12-04 16:37:48
问题 I'm trying to create a method using an expression tree that returns an object, but I can't figure out how to actually specify the object to return. I've tried reading this, but the return value doesn't actually seem to be specified anywhere. I've got all the assignments & stuff down, but how do I specify the object to return from a method created using expression trees? EDIT: these are v4 expression trees, and the method I'm trying to create does something like this: private object ReadStruct

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

妖精的绣舞 提交于 2019-12-04 16:24:23
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 prices.Select(GetPriceSelector(e => Math.Round(e.Price, 4))) Unfortunately, EF complains about it: The LINQ

Understanding Expression Tree and Parameter Evaluation

强颜欢笑 提交于 2019-12-04 15:36:14
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) { // Removed for post: Input checks and edge cases var equals = values.Select(value => (Expression)Expression

Expression of type 'System.Int64' cannot be used for return type 'System.Object'

这一生的挚爱 提交于 2019-12-04 12:59:35
I am trying to create an expression of the following form: e => e.CreationDate; CreationDate is of type long , however I want the expression to return an object instead. I want to use object as a return type because the expression is built dynamically at runtime based on a query paramater. The query parameter specifies the property to access in the expression, such as: > entities?order=creationDate > entities?order=score As you can see, I can order by different properties with different types, so a return type object would allow me to build the expression as generic as possible. The problem is

Compiled Expression Tree slow due to JIT_MethodAccessCheck

戏子无情 提交于 2019-12-04 12:57:13
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 much to say about it). Can I optimize it away somehow? JIT_MethodAccessCheck method performs security

WhereNot linq expression

一曲冷凌霜 提交于 2019-12-04 12:04:54
I am trying to create an extension "WhereNot" So I can use: Dim x = "Hello world " Dim y = x.Split.WhereNot(AddressOf String.IsNullOrEmpty) Note that my aim here is to learn linq expressions; not solve my issue. I craated this function: <Extension()> _ Public Function WhereNot(Of TElement)(ByVal source As IQueryable(Of TElement), ByVal selector As Expression(Of Func(Of TElement, Boolean))) As IQueryable(Of TElement) Return source.Where(GetWhereNotExpression(selector)) End Function I don't know how to switch the boolean flag, will the function Negate do it? answers in both vb.net and C# are

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

江枫思渺然 提交于 2019-12-04 11:54:39
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, MarkdownExporter] [MemoryDiagnoser /*, InliningDiagnoser*/] public class BenchmarkCallSimple { static Func

Is IL generated by expression trees optimized?

帅比萌擦擦* 提交于 2019-12-04 11:41:24
问题 Ok this is merely curiosity, serves no real world help. I know that with expression trees you can generate MSIL on the fly just like the regular C# compiler does. Since compiler can decide optimizations, I'm tempted to ask what is the case with IL generated during Expression.Compile() . Basically two questions: Since at compile time the compiler can produce different (may be slightly) IL in debug mode and release mode , is there ever a difference in the IL generated by compiling an expression