expression-trees

Combine BinaryExpression and Expression<Func<dynamic, bool>> in C#

前提是你 提交于 2021-02-16 13:49:08
问题 How can I combine BinaryExpression and Expression<Func<dynamic / T, bool>> ? For example: void AddGlobalFilter<T>(Expression<Func<T, bool>> expr) { var parameter = Expression.Parameter(type, "t"); var member = Expression.Property(filter.Parameter, field); var constant = Expression.Constant(null); var body = Expression.Equal(member, constant); var combine = Expression.AndAlso(body, expr); } I am trying to define global filter for Entity Framework (EF) Core. The problem is I must manually

Implicit Cast not happening in Expression Tree

老子叫甜甜 提交于 2021-02-08 13:56:49
问题 I came across a scenario where I need to sort a list of custom type on different properties based on input. With the help of few articles, I was able to come up with generic implementation using LINQ.During unit testing, one of the test failed because implicit conversion was happening when lamda expression was created using Expression tree. Below I have put the sample code to understand the issue (Not sure why formatting was not getting correct, sorry for that) static class ExtensionMethods {

How to do the convertion to Expression tree of lambda .Where() after .join() using anonymous type?

自古美人都是妖i 提交于 2021-02-08 11:39:42
问题 How to finish the convertion of the following query to Expression tree syntax, using an anonymous type in the .Where() and .Select()? IQueryable<A> As = db.A .Join( db.B, _a => _a.bID, _b => _b.ID, (a, b) => new { a, b }) //next two are the objective .Where(s=> s.b.Name == "xpto") .Select(s => s.a); At this point I have (Thanks to @NetMage): //QUERY DB with GENERIC TYPE IQueryable<B> queryable = (IQueryable<B>)db.B; // IQueryable<TOuter> var arg0 = Expression.Constant(db.A.AsQueryable()); //

Insert nodes in expression trees

无人久伴 提交于 2021-02-08 10:46:47
问题 I'm trying to evaluate an expression using a binary tree. The tree has this characteristics: Each node has zero, one or two children. Only nodes containing operators can have children. All leaf nodes must be numbers. For the sake of simplicity, the only operators allowed are * and + Something like those ones: This is my tree class: class ExpressionTree { struct Node { std::string data; Node *leftChild, *rightChild; Node(std::string d): data(d), leftChild(NULL), rightChild(NULL) {} } *root;

Force VB.NET to generate the same string comparison expression as C#?

人走茶凉 提交于 2021-02-04 19:37:45
问题 Somewhat similar question here: Difference between C# and VB.Net string comparison ...but not the same as the one I am asking now. I am creating a simple expression walker that will convert a lambda into an SQL WHERE clause. I call it like this: GetEntities<MyEntity>(e => e.MyProperty == MyValue) C# creates the expression as I would expect which is a BinaryExpression consisting of a MemberExpression on the left and a ConstantExpression on the right which looks like this: $e.MyProperty ==

Build Expression tree for LINQ to Entities Where clause

心已入冬 提交于 2021-01-28 14:13:55
问题 I want to be able to write the following code for a LINQ to Entities query (EF6) Repo.ContactRelations.WhereActive() .Select(r => new ContactModel { Id = r.Contact.Id, FirstName = r.Contact.FirstName, LastName = r.Contact.Surname, WorkEmail = r.Contact.WorkEmail }) Without the WhereActive() method, I would have to repeat the following expression in numerous places: Repo.ContactRelations.Where(c => c.EndDate == null || c.EndDate > DateTime.Today) I tried to write a simple extension method, but

Build a Generic Expression Tree .NET Core

二次信任 提交于 2021-01-28 05:16:59
问题 Hello Community i am aware of this might be a possible duplicate. How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>? https://www.strathweb.com/2018/01/easy-way-to-create-a-c-lambda-expression-from-a-string-with-roslyn/ How to create a Expression.Lambda when a type is not known until runtime? Creating expression tree for accessing a Generic type's property There are obviously too many resources. I am still confused though. Could

Parser for query filter expression tree

半腔热情 提交于 2021-01-28 03:20:33
问题 I am looking for a parser that can operate on a query filter. However, I'm not quite sure of the terminology so it's proving hard work. I hope that someone can help me. I've read about 'Recursive descent parsers' but I wonder if these are for full-blown language parsers rather than the logical expression evaluation that I'm looking for. Ideally, I am looking for .NET code (C#) but also a similar parser that works in T-SQL. What I want is for something to parse e.g.: ((a=b)|(e=1))&(c<=d)

Create Func or Action for any method (using reflection in c#)

心已入冬 提交于 2021-01-21 01:37:04
问题 My application works with loading dll's dynamically, based on settings from the database (file, class and method names). To facilitate, expedite and reduce the use of reflection I would like to have a cache.... Following the idea that using: MethodInfo.Invoke Is nothing performative ( Reflection Performance - Create Delegate (Properties C#)) I would like to translate any call to methods. I thought of something that would work like this: public static T Create<T>(Type type, string methodName)

Evaluate expression tree in Javascript

萝らか妹 提交于 2021-01-03 06:11:39
问题 I have input consisting of nested logical expression objects Ex: var obj = { 'OR': [ { 'AND': [ false, true, true ] }, { 'OR': [ true, false, false, { 'AND': [true, true] } ] }, true ] }; Which is equivalent to ((false && true && true) || (true || false || false || (true && true)) || true) We need to write a function that calculates this Approach: Go to the innermost level and evaluate it first, moving to the top var expressionEvaluator = function(opArr){ var hasChildObjects = function(arr){