expression-trees

How can I build logic upon supplied logic in a LINQ-to-Entities Where expression?

我的梦境 提交于 2020-03-06 07:28:04
问题 I often come across, in LINQ for Entity Framework, a pattern where I add a .Where clause if a string value is specified, like: IQueryable<Foo> query = Foos.AsQueryable() if (!string.IsNullOrWhitespace(nameFilter)) query = query.Where(x => x.Name == name); if (!string.IsNullOrWhitespace(addressFilter) != null) query = query.Where(x => x.Address == addressFilter ); if (!string.IsNullOrWhitespace(cityFilter) != null) query = query.Where(x => x.City == cityFilter ); // ... I wanted to clean this

How to combine (OR) two expression trees

ぃ、小莉子 提交于 2020-02-25 21:53:33
问题 I have two expression trees of type : Expression<Func<string, bool>> and I would like to obtain a single Expression that will do the OR of the two expressions (passing the same string parameter to both expressions) Any idea? 回答1: You can use PredicateBuilder from LINQKit to do this. For example: Expression<Func<string, bool>> e1 = …; Expression<Func<string, bool>> e2 = …; Expression<Func<string, bool>> combined = e1.Or(e2).Expand(); 回答2: You can try combining them in a Expression.Lambda

How to combine (OR) two expression trees

左心房为你撑大大i 提交于 2020-02-25 21:49:55
问题 I have two expression trees of type : Expression<Func<string, bool>> and I would like to obtain a single Expression that will do the OR of the two expressions (passing the same string parameter to both expressions) Any idea? 回答1: You can use PredicateBuilder from LINQKit to do this. For example: Expression<Func<string, bool>> e1 = …; Expression<Func<string, bool>> e2 = …; Expression<Func<string, bool>> combined = e1.Or(e2).Expand(); 回答2: You can try combining them in a Expression.Lambda

manually build linq expression for x => x.Child == itemToCompare.Child

喜夏-厌秋 提交于 2020-01-29 10:02:29
问题 We have an object and we want to build a linq query based on that object on the fly. This linq statement is equivalent to what we want to build: Expression<Func<Sample, bool>> linqExpression = x => x.Child == itemToCompare.Child; We can't quite come up with the right expression to build the itemToCompare.Child part. Here's what we have so far: var param = Expression.Parameter(typeof(T), "x"); var key = itemToCompare.GetType().GetProperty("Child"); var rhsConstant = Expression.Constant(item);

Mutating the expression tree of a predicate to target another type

你离开我真会死。 提交于 2020-01-26 08:53:08
问题 Intro In the application I 'm currently working on, there are two kinds of each business object: the "ActiveRecord" kind and the "DataContract" kind. So for example, there would be: namespace ActiveRecord { class Widget { public int Id { get; set; } } } namespace DataContract { class Widget { public int Id { get; set; } } } The database access layer takes care of translating between families: you can tell it to update a DataContract.Widget and it will magically create an ActiveRecord.Widget

Mutating the expression tree of a predicate to target another type

十年热恋 提交于 2020-01-26 08:48:24
问题 Intro In the application I 'm currently working on, there are two kinds of each business object: the "ActiveRecord" kind and the "DataContract" kind. So for example, there would be: namespace ActiveRecord { class Widget { public int Id { get; set; } } } namespace DataContract { class Widget { public int Id { get; set; } } } The database access layer takes care of translating between families: you can tell it to update a DataContract.Widget and it will magically create an ActiveRecord.Widget

Create Expression from PropertyInfo

僤鯓⒐⒋嵵緔 提交于 2020-01-22 18:53:08
问题 I'm using an API that expects an Expression<Func<T, object>> , and uses this to create mappings between different objects: Map(x => x.Id).To("Id__c"); // The expression is "x => x.Id" How can I create the necessary expression from a PropertyInfo ? The idea being: var properties = typeof(T).GetProperties(); foreach (var propInfo in properties) { var exp = // How to create expression "x => x.Id" ??? Map(exp).To(name); } 回答1: You just need Expression.Property and then wrap it in a lambda. One

Create Expression from PropertyInfo

风流意气都作罢 提交于 2020-01-22 18:52:26
问题 I'm using an API that expects an Expression<Func<T, object>> , and uses this to create mappings between different objects: Map(x => x.Id).To("Id__c"); // The expression is "x => x.Id" How can I create the necessary expression from a PropertyInfo ? The idea being: var properties = typeof(T).GetProperties(); foreach (var propInfo in properties) { var exp = // How to create expression "x => x.Id" ??? Map(exp).To(name); } 回答1: You just need Expression.Property and then wrap it in a lambda. One

Trying to filter on a Nullable type using Expression Trees

拈花ヽ惹草 提交于 2020-01-22 16:53:06
问题 I have pasted my entire test app below. It's fairly compact so I am hoping that it's not a problem. You should be able to simply cut and paste it into a console app and run it. I need to be able to filter on any one or more of the Person objects' properties, and I don't know which one(s) until runtime. I know that this has beed discussed all over the place and I have looked into and am also using tools such as the PredicateBuilder & Dynamic Linq Library but the discussion aroung them tends to

Trying to filter on a Nullable type using Expression Trees

扶醉桌前 提交于 2020-01-22 16:52:50
问题 I have pasted my entire test app below. It's fairly compact so I am hoping that it's not a problem. You should be able to simply cut and paste it into a console app and run it. I need to be able to filter on any one or more of the Person objects' properties, and I don't know which one(s) until runtime. I know that this has beed discussed all over the place and I have looked into and am also using tools such as the PredicateBuilder & Dynamic Linq Library but the discussion aroung them tends to